React hooks 如何使用React钩子获取按钮单击时的数据

React hooks 如何使用React钩子获取按钮单击时的数据,react-hooks,React Hooks,我想在click on Button上显示子组件,但是在这里,我在页面加载时获取数据,我需要编写一个条件,比如单击Button时,然后只有子组件显示,否则默认文本应该显示在那里 const DetailsCard = () => { const [employee, setemployee] = useState([]) const [data, setData] = useState() useEffect(() => { fetch(&

我想在click on Button上显示子组件,但是在这里,我在页面加载时获取数据,我需要编写一个条件,比如单击Button时,然后只有子组件显示,否则默认文本应该显示在那里

const DetailsCard = () => {

    const [employee, setemployee] = useState([])
    const [data, setData] = useState()

    useEffect(() => {
        fetch("http://localhost:3000/users").then(res => res.json()).then((result) => {
            setemployee(result);
        }, [])
    })

    const handleChange = () => {

        setData(true);

    }

    return (
        <div>
            <h1>LordShiva</h1>
            <div className="container">
                <div className="row">
                    <div className="col">
                        <div className="card">
                            <div className="card-header bg-primary text-white">
                                <p className="h4">Birthday APP</p>
                                <button className="btn btn-red true1 " onClick={handleChange} >HappyBirthday</button>
                            </div>
                            <div className="card-body">
                                <TableCard data={employee} />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    )
}

export default DetailsCard;
const DetailsCard=()=>{
const[employee,setemployee]=useState([])
const[data,setData]=useState()
useffect(()=>{
取回(“http://localhost:3000/users)然后(res=>res.json())。然后((结果)=>{
setemployee(结果);
}, [])
})
常量handleChange=()=>{
设置数据(真);
}
返回(
主湿婆
生日应用程序

生日快乐 ) } 导出默认详细信息;
const DetailsCard=()=>{
const[employee,setemployee]=useState([]);
const[showChild,toggleShowChild]=useState(false);
useffect(()=>{
取回(“http://localhost:3000/users")
.然后((res)=>res.json())
。然后((结果)=>{
setemployee(结果);
}, []);
});
常量handleChange=()=>{
切换showChild(!showChild);
};
返回(
主湿婆
生日应用程序

生日快乐 {/*仅当'showChild'为true时显示子项。*/} {showChild&&( )} ); }; 导出默认详细信息;

const DetailsCard = () => {
  const [employee, setemployee] = useState([]);
  const [showChild, toggleShowChild] = useState(false);

  useEffect(() => {
    fetch("http://localhost:3000/users")
      .then((res) => res.json())
      .then((result) => {
        setemployee(result);
      }, []);
  });

  const handleChange = () => {
    toggleShowChild(!showChild);
  };

  return (
    <div>
      <h1>LordShiva</h1>
      <div className="container">
        <div className="row">
          <div className="col">
            <div className="card">
              <div className="card-header bg-primary text-white">
                <p className="h4">Birthday APP</p>
                <button className="btn btn-red true1 " onClick={handleChange}>
                  HappyBirthday
                </button>
              </div>

              {/* Only show child when `showChild` is true. */}
              {showChild && (
                <div className="card-body">
                  <TableCard data={employee} />
                </div>
              )}
            </div>
          </div>
        </div>
      </div>
    </div>
  );
};

export default DetailsCard;