Hi FE !
Ai
git
前端面试题
前端小tip
  • vite
  • webpack
npm
  • vue2
  • vue3
react
GitHub
Ai
git
前端面试题
前端小tip
  • vite
  • webpack
npm
  • vue2
  • vue3
react
GitHub
  • 子组件调用父组件方法

子组件调用父组件方法

import React,{Component} from "react"
//子组件
class Son extends Component{
  constructor(props){
    super(props);
  }
  render(){
    const {isShow} =this.props;
    return(
      <div onClick={isShow}>
          我是子组件
       </div>
    )
  }
}

export default class sonCallfather extends Component{
    constructor(props){
      super(props);
      this.state={
        isshow:true
      }
    }

    hideSon =() =>{
      this.setState({
        isshow:false
      });
    }

    showSon =() =>{
      this.setState({
        isshow:true
      });
    }

    render(){
      const {isshow} =this.state;
      return(
        <div>
            子组件在父组件里面展示
            {isshow ? <Son isShow={this.hideSon}/> :
              <div onClick={this.showSon}>
                    我影藏了,点我就出来
              </div>}  
        </div>
      )
    }
}
Edit this page
最近更新: 2025/6/27 02:24
Contributors: qdleader
qdleader
本站总访问量 129823次 | 本站访客数 12人