in Education by
I'm a bit surprised I'm having trouble finding this online, but I can't seem to find an example of how to do this in a React functional component. I have a React component that I would like to render when I click a button. Right now the function fires and I can see my console.log firing, however the component isn't rendering. My first guess was that it won't render because React doesn't know to update the view, however I added boolean via useState and it still won't render. What am I doing wrong? Below is the relevant code. How can I get the component in addSection to render? const FormGroup = ({index}) => { const [additionalSection, setAdditionalSection] = useState(false); const addSection = form => { setAdditionalSection(true); console.log('form', form); return additionalSection && (

This should render

); }; ... return ( ... addSection(form)} > span className="button--small">{form.button} ); 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
You should change your state (or a prop in your useEffect dependency array in case you had one) in order to force a rerender. In this case: setAdditionalSection(prevState=>!prevState);

Related questions

0 votes
    I'm a bit surprised I'm having trouble finding this online, but I can't seem to find an example ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I'm a bit surprised I'm having trouble finding this online, but I can't seem to find an example ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Hi I have to pass array as props to a functional component. Show code snippet But it is converted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Hi I have to pass array as props to a functional component. Show code snippet But it is converted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    Hi I have to pass array as props to a functional component. Show code snippet But it is converted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 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
    This is my object. values : { title : 'this is title ..', translate : { en : 'some texts ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    This is my object. values : { title : 'this is title ..', translate : { en : 'some texts ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    I am trying to calculate the progress of a file upload, so I was using Superagent. I was able to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    I have a Composite Component that uses ajax to change multiple elements when an item is selected. I am ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    What is the virtual DOM? How does react use the virtual DOM to render the UI?...
asked Dec 5, 2020 in Technology by JackTerrance
0 votes
    Can one make an angular application to render on the server-side?...
asked Jun 30, 2021 in Education by JackTerrance
0 votes
    In Haskell, Iteratee based I/O seems very attractive. Iteratees are a composable, safe, fast ways of doing ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    On the left you will notice the google logo rendered by IE, I drew a black line at the top and ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
...