r/reactjs • u/timmonsjg • Dec 03 '18
Needs Help Beginner's Thread / Easy Questions (December 2018)
Happy December! βοΈ
New month means a new thread π - November and October here.
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?
π Here are great, free resources! π
38
Upvotes
View all comments
2
u/NickEmpetvee Dec 03 '18
Question about lifting up state and controlled components. If we have a simple login form like the below with credentials getting processed in a
<parent />component functionauth(uid, pw), is it appropriate to still keep the UID/PW in local state for the controlled component or better to lift them up to<parent />? My thought is that they should stay in the local state of<LoginForm />because they're tied to its form fields, and passed in tothis.prop.auth(this.state.uid,this.state.pw)but there's something I may not be seeing.class LoginForm extends React.Component {constructor(props) {super(props);this.state = {uid: '', pw: ''};}handleUIDChange = (event) => {this.setState({value: event.target.uid});}handlePWChange = (event) => {this.setState({value:event.target.pw});}handlePWSubmit = (event) => {this.props.auth(this.state.uid,this.state.pw)}render() {return (<form onSubmit={this.handleSubmit}><label>User ID:<input type="text" value={this.state.uid} onChange={this.handleUIDChange} /></label><label><input type="password" value={this.state.pw} onChange={this.handlePWChange} /></label><input type="submit" value="Submit" /></form>);}}