react的子传父

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id='box'></div>
    <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script><!--react的核心库-->
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script><!--提供操作dom的扩展库-->
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script><!--解析jsx语法代码为纯js语法代码的库-->
    <script type="text/babel">
     /*
        子传父(函数传进去给到子元素,子元素通过调用父元素的函数,使得修改父元素得数据)
        调用父元素的函数,从而修改父元素的数据,从而实现数据从子元素传至父元素
    */ 
    class Father extends React.Component{
        constructor(){
            super()
            this.state = {
                name: 'lsh'
            }
        }
        render(){
            return (
                      <div>
                            <h1>{this.state.name}</h1>
                            {/* <Son setChildData={this.setChildData}/>*/}
                            
                            <Son setFun={this.setChildData}/>
                      </div>
                   )
        }
        // 设置函数修改状态,该函数可以修改子元素的数据,这个函数传递给子元素
        setChildData=(data)=>{
            this.setState({name:data})
        }
     }
     class Son extends React.Component{
        constructor(props){
            super(props)
            this.state = {
                msg: 'react',
                msg1: 'reactVue'
            }
        }
        render(){
            return(
                <div>
                    {/*点击button将子元素的内容传给父元素*/}
                    {/*<button onClick={this.setData}>传递helloworld给父元素</button>*/}
                    <button onClick={this.setData}>传递helloworld给父元素</button>
                    {/*调用props是父元素的函数,this.props.setFun这种不好传参数  */}
                    <button onClick={()=>{this.props.setFun('直接调用props的函数')}}>传递helloworld给父元素</button>
                    <button onClick={()=>{this.props.setFun(this.state.msg1)}}>传递helloworld给父元素</button>
                </div>
            )
        }
        // 方法写在函数外面,setData发送数据,拿到this.state这个数据
        // this.setData = this.setData.bind(this)
        setData=()=>{
            // {...this.state,name:'ls'} 将原有的对象进行展开,并且将原来的对象进行合并
            // 获取当前的方法  this.props 指的是当前传递的数据或者方法
            // console.log(this.props, this.props.setFun, 'this.props')
            // 将子元素传递给父元素,实际就是调用父元素传递进来的父元素函数
            this.props.setFun(this.state.msg)
        }
     }
     ReactDOM.render(
         <Father/>,
         // document.querySelector('#box') 是选中元素
         document.getElementById('box')
     )
    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值