in Education by
So I am having trouble with setting the state for the date selected from this DatePicker change: change = (e) => { this.setState({ [e.target.name]: e.target.value, }); }; DatePicker component: this.change(e)} /> If I use the default date when you first reload the page, there is no issue. But it's when a date is selected that I get an error. A Type Error: Cannot read property 'name' of undefined. What am I doing wrong? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The SyntheticEvent is pooled. This means that the SyntheticEvent object will be reused and all properties will be nullified after the event callback has been invoked. This is for performance reasons. As such, you cannot access the event in an asynchronous way. More information can be found here. Since setState is asynchronous the event will be null by the time it's evaluated in setState. Instead, deconstruct the event before setState: change = ({ target: { name, value } }) => { this.setState({ [name]: value, }); } or change = e => { const { name, value } = e.target; this.setState({ [name]: value, }); }

Related questions

0 votes
    So I am having trouble with setting the state for the date selected from this DatePicker change: change = ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    I recently began working on a project which has many gridviews on a single page. During creation of a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    I recently began working on a project which has many gridviews on a single page. During creation of a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    I recently began working on a project which has many gridviews on a single page. During creation of a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    Solve the following puzzle You are going to visit your grandmother on Mother's day. You want to give her two cakes. ... you leave with to be able to give your grandmother 2 cakes...
asked Feb 12, 2021 in Education by JackTerrance
0 votes
    A card is selected from a deck of 52 cards. The probability of its being a red face card is (A) 3/26 (B) 3/13 (C) 2/13 (D) 1/2 Select the correct answer from above options...
asked Nov 21, 2021 in Education by JackTerrance
0 votes
    I have following code and I want to get data from service. I have set everything from what I get ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    I have following code and I want to get data from service. I have set everything from what I get ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
0 votes
    With a chrome plugin, I changed my timezone to +14. However, I saw that the calendar app is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    i have an Angular Factory that gets a single date from the backend of my spring application, and i ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
0 votes
0 votes
    Two integers are selected at random from the set {1,2, ..,11}. Given that the sum of selected numbers is even, the ... D. `(3)/(5)` Select the correct answer from above options...
asked Nov 13, 2021 in Education by JackTerrance
...