验证码: 看不清楚,换一张 查询 注册会员,免验证
  • {{ basic.site_slogan }}
  • 打开微信扫一扫,
    您还可以在这里找到我们哟

    关注我们

react组件传值的方法有哪些

阅读:304 来源:乙速云 作者:代码code

react组件传值的方法有哪些

1、父向子传值

父组件

classAppextendsReact.Component{
    state={
        name:'jack',
        age:19,
        gender:'男',
        count:1
    }
    render() {
        return
            御剑乘风来,除魔天地间!
            
        
    }

子组件:

  const Child = (props) => {
    console.log(props,99);
    return
      御剑乘风来,除魔天地间!===Child======{props.count}     
    };

2、子向父传值

父组件

classAppextendsReact.Component{
    state={
        name:'jack',
        age:19,
        gender:'男',
        count:1
    }
  handle2=(msg)=>{
        console.log(msg,77); //  123
    }
    render() {
        return
            御剑乘风来,除魔天地间!==
            
        
    }

子组件:

  const Child = (props) => {
    console.log(props,99);
    var handle=()=>{
            console.log(111);
      props.getMsg('123')
        }
    return
      御剑乘风来,除魔天地间!===Child======         child1
    };

3、非父子传值

child1 向 child2 传值

思路:child  传到app  ,然后app传到child2

classAppextendsReact.Component{
    state={
        name:'jack',
        age:19,
        gender:'男',
        count:1
    }
  handle2=(msg)=>{
        console.log(msg,77); //  123this.setState({
      count:this.state.count+msg
    })
    }
    render() {
        return
            御剑乘风来,除魔天地间!==
            
        
        
    }

子组件1:

const Child = (props) => {
    console.log(props,99);
    var handle=()=>{
            console.log(111);
      props.getMsg('123')
        }
    return
      御剑乘风来,除魔天地间!===Child======         child1
    };   const Child2 = (props) => {     console.log(props,99);     var handle=()=>{             console.log(111);       props.getMsg('123')         }     return
      御剑乘风来,除魔天地间!===Child======         child1
    };

4、Context 方法 传值 【类似vue的 provide / inject】

步骤:

1- const  {Provider,Consumer} = React.createContext()
2- Provider包裹父组件 定义value是需要传的值  ====
3- Consumer包裹需要接收数据的组件 data接收数据


            {data=>(
                    
                        御剑乘风来,除魔天地间! ==Child2*****{data}                                     
            )}

代码如下:

import React from 'react'const  {Provider,Consumer} = React.createContext()
const Child = (props) => {
    console.log(props,99);
    return 
        御剑乘风来,除魔天地间!===Child
};  classChild2extendsReact.Component{     render() {         return 
                             {data=>(                     
                        御剑乘风来,除魔天地间! ==Child2*****{data}                                     
            )}                      
    } }  classAppextendsReact.Component{     state={         name:'jack',         age:19,         gender:'男',         count:1     }     render() {         return              御剑乘风来,除魔天地间!=={this.state.name}                                        } } export default App
分享到:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: hlamps#outlook.com (#换成@)。
相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>