简体   繁体   English

如何在 React js 中单击按钮时调用组件?

[英]How to call component on button click in React js?

In React Js when I call child component within Parent component then component call render time but I want to call child component when I will click on button then call child component.在 React Js 中,当我在父组件中调用子组件然后组件调用渲染时间但我想在单击按钮然后调用子组件时调用子组件。 I don't want to use routing.我不想使用路由。

I tried Props and context API but It is nor worked properly.我尝试了道具和上下文 API 但它也没有正常工作。

do this create a state which initially is false and with component which you call make it conditional with optional chaining and the button onclick changes the state to true so the component is rendered这样做会创建一个 state ,它最初是 false 并且使用您调用的组件使其具有可选链接的条件并且按钮 onclick 将 state 更改为 true 以便呈现组件

something like this像这样的

import React,{useState} from 'react'

const abc = () => {
    const [isrendered,setisrendered]=useState(false);
  return (
    <div>abc</div>
    <input type="button" onClick={()=>{setisrendered(true)}} />click this to show component
    {isrendered&&<Thecomponent/>}
  )
}

export default abc

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

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