Javascript Cannot Read Property of Push of Undefined
The issue Cannot read the property of undefined is quite mutual in JavaScript. When the variable is non defined, JavaScript can non read the undefined variable or holding. A similar issue we can discover in React JS. We clarify the consequence and endeavour to discover a solution.

import React from 'react'; import * as courseActions from '../Redux/actions/courseAction'; import propTypes from 'prop-types'; import { connect } from 'react-redux'; class CoursePage extends React.Component { state = { course: { title: '', }, }; handlechange (event) { const course = { ...this.state.course, title: upshot.target.value }; this.setState({ grade }); }; handleSubmit(upshot) { outcome.preventDefault(); this.props.dispatch(courseActions.createCourse(this.country.course)); }; render() { render ( <form onSubmit={this.handleSubmit}> <h2>courses</h2> <h3>Add course</h3> <input type='text' onChange={this.handlechange} value={this.state.grade.title} /> <input type='submit' value='save' /> </class> ); } }
Solution
The effect hither is not nosotros what expect i.east. 'Cannot read holding 'state' of undefined', There are 3 approaches in context binding in react.
handlechange() { const form = { …this.state.course, title: issue.target.value }; this.setState({ grade }); }
Approach 1: Demark 'this
' context to own example – Less preferred
class CoursePage extends React.Component { state = { course: { title: '', }, }; handlechange() { const course = { …this.country.course, title: upshot.target.value }; this.setState({ course }); } return(){ return ( <class> <h2>courses</h2> <h3>Add course</h3> <input type='text' onChange={this.handlechange.demark(this)} value={this.state.course.title} /> <input type='submit' value='save' /> </course> ); } }
This works, because now this context bound to our class context, i.e {this.handlechange.bind(this)}
to the onChange
consequence, but It volition cause to create an in-part unnecessarily on each render…
Approach ii : Binding in the Constructor – Less preferred than Pointer function
The near common approach volition be to do it in theconstructor
because it runs simply once, now the part is jump only once, this is called context-bounden in the constructor, this approach is preferable, won't be reallocated on each render
grade CoursePage extends React.Component { constructor(props) { super(props); this.state = { class: { title: "", }, }; //binding in constuctor this.handlechange= this.handlechange.demark(this); }; handlechange() { const course = { …this.land.course, title: consequence.target.value }; this.setState({ grade }); } return ( <form> <h2>courses</h2> <h3>Add form</h3> <input type='text' onChange={this.handlechange} value={this.state.course.championship} /> <input blazon='submit' value='save' /> </course> ); } }
performance wise it is better to bind in the constructor. The demark in constructor method will create a single instance of the part and re-use it, even if the render method is chosen multiple times
Approach three: Pointer functions – More preferred
Inherit the binding context of their enclosing scope. basically, they don't accept 'this' binding, 'this' keyword inside references the class instance.
form CoursePage extends React.Component { land = { course: { title: '' } }; handlechange = (event) => { const course = { ...this.state.form, championship: upshot.target.value }; this.setState({ course }); }; render() { render ( <class> <h2>courses</h2> <h3>Add class</h3> <input type='text' onChange={this.handlechange} value={this.land.course.championship} /> <input blazon='submit' value='salve' /> </form> ); }
Tip: How the Arrow function preferred in react land to achieve context binding?
Arrow functions are great. They do expect nicer, But at that place is a much amend reason to apply arrow functions. It would be more efficient if nosotros know, when and where you are using it.
handlechange = (issue) => { const form = { ...this.country.course, championship: issue.target.value }; this.setState({ course }); };
In above example, the arrow function does not recreate function on each return, Similar to how binding in the constructor works in 2nd approach.
Source: https://techstrology.com/cannot-read-property-state-of-undefined-reactjs/
0 Response to "Javascript Cannot Read Property of Push of Undefined"
Post a Comment