0

I want to render child component when I click the button
Parents

const Parents:React.FC<PropTypes> = ({inputs}) => {
  return(
    <div>
      <Button onClick={() => <Child props={inputs}/>}
    </div>
   )}

Child

const Child:React.FC<AnotherPropTypes> = ({props}) => {
  // ... 
}

I am using React, TypeScript and Material-UI for it.
My question is that, it does not seem like onClick event trigger Child component. How can I run child component when I click the button?
Any help appreciated!

1 Answer 1

5

Add state to your component. The click event sets the state, and the state is used to decide whether or not to render extra things.

const Parents:React.FC<PropTypes> = ({inputs}) => {
  const [showChild, setShowChild] = useState(false);
  return(
    <div>
      <Button onClick={() => setShowChild(true)} />
      {showChild && <Child props={inputs}/>}
    </div>
  )
}
Sign up to request clarification or add additional context in comments.

1 Comment

Is it the React concept?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.