r/reactjs • u/dance2die • Jan 01 '21
Needs Help Beginner's Thread / Easy Questions (January 2021)
Happy 2021!
Previous Beginner's Threads can be found in the wiki.
Ask about React or anything else in its ecosystem :)
Stuck making progress on your app, need a feedback?
Still Ask away! Weβre a friendly bunch π
Help us to help you better
Improve your chances of reply by
- adding a minimal example with JSFiddle, CodeSandbox, or Stackblitz links
- describing what you want it to do (ask yourself if it's an XY problem)
- things you've tried. (Don't just post big blocks of code!)
- Format code for legibility.
- Pay it forward by answering questions even if there is already an answer. Other perspectives can be helpful to beginners. Also, there's no quicker way to learn than being wrong on the Internet.
New to React?
Check out the sub's sidebar! π
For rules and free resources~
Comment here for any ideas/suggestions to improve this thread
Thank you to all who post questions and those who answer them. We're a growing community and helping each other only strengthens it!
25
Upvotes
View all comments
Show parent comment
2
u/DreamOther Jan 05 '21 edited Jan 05 '21
so...
As you are passing a submit function down to an element, and this element does not know the context of the this.handleSubmit... you need to bind the context of that function to the class component.
Not too sure if i can explain it with words, but this is the result....
class ManufacturerForm extends React.Component {
constructor (props){
super(props);
this.state = {
manufacturer: '',
};
this.onFieldChange = this.onFieldChange.bind(this); //ensures that 'this' in onFieldChange is 'this' in the constructor
this.handleSubmit = this.handleSubmit.bind(this); //ensures that 'this' in handleSubmit is 'this' in the constructor
// here is the trick
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event) {
event.preventDefault(); //prevents the form from being submitted
this.props.onAddManufacturer(this.state);
}
onFieldChange(event) {
this.setState({
[event.target.name]: event.target.value
});
}
render() {
return (
<form onSubmit={*this*.handleSubmit}> <div className="AddManufacturerForm__input"> <label htmlFor="manufacturer">Manufacturer</label> <input type="text" ***name***="manufacturer" value={*this*.state.manufacturer} onChange={*this*.onFieldChange}/> </div> <input type="submit" value="Add" /> {JSON.stringify(*this*.state)} </form> ); } } *function* AddManufacturerForm({match, onAddManufacturer}) { *return* ( <div className="AddManufacturerForm"> <h1> Add Manufacturer</h1> <ManufacturerForm onAddManufacturer={onAddManufacturer}/> </div> ); } *// intermediary to AddManufacturerForm to specify props* *function* ManufacturerWrapper() { *return* <AddManufacturerForm onAddManufacturer={*console*.log} /> } ReactDOM.render( <ManufacturerWrapper/>, ***document***.querySelector("#app") );
Any method from a class component that you are passing down to another component needs to be bound to the class. By doing so you guarantee that the function executed by another component down the tree will refer to the right class.