简体   繁体   English

如何在 reactjs 中使用 onclick 事件渲染子组件?

[英]How to render child component with onclick event in reactjs?

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.我正在为此使用 React、TypeScript 和 Material-UI。
My question is that, it does not seem like onClick event trigger Child component.我的问题是,它似乎不像onClick事件触发子组件。 How can I run child component when I click the button?单击按钮时如何运行子组件?
Any help appreciated!任何帮助表示赞赏!

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>
  )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM