r/reactjs • • Jun 02 '19

Beginner's Thread / Easy Questions (June 2019)

Previous two threads - May 2019 and April 2019.

Got questions about React or anything else in its ecosystem? Stuck making progress on your app? Ask away! We’re a friendly bunch.

No question is too simple. πŸ€”


πŸ†˜ Want Help with your Code? πŸ†˜

  • Improve your chances by putting a minimal example to either JSFiddle or Code Sandbox. Describe what you want it to do, and things you've tried. Don't just post big blocks of code!

  • Pay it forward! Answer questions even if there is already an answer - multiple perspectives can be very helpful to beginners. Also there's no quicker way to learn than being wrong on the Internet.

Have a question regarding code / repository organization?

It's most likely answered within this tweet.


New to React?

Check out the sub's sidebar!

πŸ†“ Here are great, free resources! πŸ†“


Any ideas/suggestions to improve this thread - feel free to comment here!


Finally, an ongoing thank you to all who post questions and those who answer them. We're a growing community and helping each other only strengthens it!

32 Upvotes

394 comments sorted by

View all comments

Show parent comment

1

u/Awnry_Abe Jun 05 '19

There is no such thing as a non-reactive value of a prop. If you expose more code, we could give an assist. The solution will involve capturing props as local state, and tightly governing the transition logic around isEditing and whether that prop has been captured already.

1

u/svenjoy_it Jun 06 '19

Here's my full component.

import React, { useState } from 'react';
import PropTypes from "prop-types";
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';

import './RuntimeDisplay.css';
import makeStyles from "@material-ui/core/styles/makeStyles";

const RuntimeDisplay = (props) => {

	const [editing, setEditing] = React.useState(false);


	const [hour, setHour] = React.useState(0);
	const [minute, setMinute] = React.useState(0);
	const [second, setSecond] = React.useState(0);


	const startEditing = () => {
		let t = props.time;
		const h = Math.floor(t / 3600);
		t %= 3600;
		const m = Math.floor(t / 60);
		const s = t % 60;

		setHour(h);
		setMinute(m);
		setSecond(s);
		setEditing(true);
	};


	const runningTimePretty = () => {
		let t = props.time;
		const h = Math.floor(t / 3600);
		t %= 3600;
		const m = Math.floor(t / 60);
		const s = t % 60;

		const sh = h < 10 ? "0" + h.toString() : h.toString();
		const sm = m < 10 ? "0" + m.toString() : m.toString();
		const ss = s < 10 ? "0" + s.toString() : s.toString();

		return sh + ":" + sm + ":" + ss;
	};

	const editStyle = makeStyles(theme => ({
		root: {
			border: '1px solid #e2e2e1',
			overflow: 'hidden',
			borderRadius: 4,
			backgroundColor: '#fcfcfb',
			transition: theme.transitions.create(['border-color', 'box-shadow']),
			'&:hover': {
				backgroundColor: '#fff',
			},
			'&$focused': {
				backgroundColor: '#fff',

				borderColor: theme.palette.primary.main,
			},
		},
		focused: {},
	}));

	function EditTimeInput(props) {
		const classes = editStyle();

		return <TextField className={"edit-time-input"} type={"number"} variant="filled" InputProps={{ classes, disableUnderline: true }} {...props} />;
	}

	if (editing) {
		return (
			<div className={"timer timer-edit"}>
				<EditTimeInput
					label="Hours"
					value={hour}
				/>
				<EditTimeInput
					label="Minutes"
					value={minute}
				/>
				<EditTimeInput
					label="Seconds"
					value={second}
				/>

				<Button onClick={() => saveTime()} variant="contained" color="primary" className={"time-edit-save-btn"}>
					Save
				</Button>
				<Button onClick={() => setEditing(false)} variant="contained" color="secondary" className={""}>
					Cancel
				</Button>
			</div>
		)
	}
	return (
		<div onClick={() => startEditing()} className={"timer"}>{runningTimePretty()}</div>
	)
};

RuntimeDisplay.propTypes = {
	time: PropTypes.number,
};

export default RuntimeDisplay;

And my parent component has an ever incrementing timer and return this:

<RuntimeDisplay time={props.time} />