简体   繁体   中英

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. I don't want to use routing.

I tried Props and context API but It is nor worked properly.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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