in Education by
I have have no error in my code. But my condition, finally don't work. const togglePeronsHandler = () => { const doesShow = personsState.showPersons; personsState.showPersons = !doesShow; console.log(personsState); } (...)
{ personsState.showPersons === true ? ( personsState.persons.map( (item, index) => ( )) ) : (
No body
) } When I click on button, personsState.showPersons passed true/false. But rendering it wasn't effect… At origin, i have change setPersonState but it didn't do anything... Sandbox : https://codesandbox.io/s/3yn05lro81 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
Since personsState is immutable, you can't re-assign it. You have to set it with setPersonsState like you did in the other functions. I see you need the previous state in order to do that (that's probably where your confusion came from). Apart from the object syntax of setState({...newState}) there is another syntax that takes a callback function: setState(previousState => { return {...newState} }). This is solved using this callback setState syntax. This way, your togglePersonsHandler function would look like this: const togglePersonsHandler = () => { setPersonsState(previousPersonsState => { const doesShow = previousPersonsState.showPersons; return { ...previousPersonsState, showPersons: !doesShow }; }); }; Good luck!

Related questions

0 votes
    I have have no error in my code. But my condition, finally don't work. const togglePeronsHandler = ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I have have no error in my code. But my condition, finally don't work. const togglePeronsHandler = ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: useState set method not reflecting change immediately (17 answers) Closed ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: useState set method not reflecting change immediately (17 answers) Closed ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I have some website which requires a logon and shows sensitive information. The person goes to the page, is ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    The search algorithm which is similar to the minimax search, but removes the branches that don't affect the final ... Breadth-first search Alpha-beta pruning None of the above...
asked Mar 8, 2021 in Technology by JackTerrance
0 votes
    I have a class which has 2 sets of getter & setters. 1 set is the traditional type. These work ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 30, 2022 in Education by JackTerrance
0 votes
    recently i'm working on some project with arm but no OS in it. Now when i compile it, i must ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I'm experiencing some problems regarding Twitter OAuth within an android activity. I read a lot of tutorials ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I have been using php-sdk to connect to facebook for a while and I never had any problems, but ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    Each of a pair of transactions has read data that is written by the other, but there is no data ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    What has a neck but no head and wears a cap?...
asked Feb 11, 2021 in Education by JackTerrance
0 votes
    What has a spine but no bones? A) Mirror B) Book C) Table D) None of the above...
asked Feb 11, 2021 in Education by JackTerrance
0 votes
    URL is http://*.*.*.*/100/?id=1&version=1 params is {"cityId": "110000", "query": {" ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 27, 2022 in Education by JackTerrance
...