美文网首页
react组件引用传值

react组件引用传值

作者: 胡齐峰 | 来源:发表于2020-11-05 18:30 被阅读0次

组件引用

  • ref:需要引用的组件
  • refs:父级中引用的所有ref组件
  • 子组件中的ref不能重复,如果重复了在refs中会进行覆盖。
class Parent extends React.Component{
    constructor(...args){
        super(...args)
    }
    componentDidMount() {
        console.log(this.refs);
    }
    render(){
        return (
            <div>
                <h2>父组件</h2>
                <Child ref="Child1"></Child>
                <Child ref="Child2"></Child>
            </div>
        )
    }
}
class Child extends React.Component{
    constructor(...args){
        super(...args)
        this.state={
            a:1
        }
    }
    render(){
        return (
            <div>
                <h2>子组件</h2>
                <p>{this.state.a}</p>    
            </div>
        )
    }
}
ReactDOM.render(<Parent></Parent>,document.getElementById('root'))

父组件传值子组件

  • 在父组件中的refs中对相应的子组件进行setState,不推荐使用
  • 在子组件中添加对应的传值的方法,在父组件中调用
class Parent extends React.Component{
    constructor(...args){
        super(...args)
    }
    componentDidMount() {
        console.log(this.refs);
    }
    fn(){
        //不建议在父组件中直接进行子组件的状态设置,可以在子组件中暴露一个方法,在父组件中调用传参等
        // this.refs.Child1.setState({
        //     a: this.refs.Child1.state.a + parseInt(this.refs.text.value)
        // })
        const {text,Child1} = this.refs
        Child1.add(text.value)
    }
    render(){
        return (
            <div>
                <h2>父组件</h2>
                <input type="text" ref="text"></input>
                <input type="button" value="加1" onClick={this.fn.bind(this)}></input>
                <Child ref="Child1"></Child>
            </div>
        )
    }
}
class Child extends React.Component{
    constructor(...args){
        super(...args)
        this.state={
            a:1
        }
    }
    add(n){
        this.setState({
            a:this.state.a + parseInt(n)
        })
    }
    render(){
        return (
            <div>
                <h2>子组件</h2>
                <p>{this.state.a}</p>    
            </div>
        )
    }
}
ReactDOM.render(<Parent></Parent>,document.getElementById('root'))

子组件传值父组件

  • 在父组件中使用使用子组件的时候将父组件中的this通过props传给子组件
  • 在子组件中获取传递的props中的父组件的this对象,再调用相应的父组件中的方法传值就可以了
class Parent extends React.Component{
    constructor(...args){
        super(...args)
        this.state={
            a:1
        }
    }
    add(n){
        this.setState({
            a:this.state.a + parseInt(n)
        })
    }
    render(){
        return (
            <div>
                <h2>父组件</h2>
                <p>{this.state.a}</p>  
                <Child  parent={this}></Child>
            </div>
        )
    }
}
class Child extends React.Component{
    constructor(...args){
        super(...args)
    }
    fn(){

        this.props.parent.add(parseInt(this.refs.text.value))
    }
    render(){
        return (
            <div>
                <h2>子组件</h2>
                <input type="text" ref="text"></input>
                <input type="button" value="加1" onClick={this.fn.bind(this)}></input>
            </div>
        )
    }
}
ReactDOM.render(<Parent></Parent>,document.getElementById('root'))

非父子组件传值

  • 同页面内:公共对象、共同父级中转
  • 跨页面:localhost、服务器中转
  • redux 能够在程序中共享数据

相关文章

  • react组件引用传值

    组件引用 ref:需要引用的组件 refs:父级中引用的所有ref组件 子组件中的ref不能重复,如果重复了在re...

  • react子组件向父组件传值

    相关资料:react 父组件怎么获取子组件的这个值React组件间信息传递方式react同级组件之间传值 • 父...

  • react-父子组件间通信

    React-父子组件间通信 父组件传 值 给子组件, 传 方法 给子组件 子组件接收 值 ,触发父组件的 方法

  • Vue中父子组件,爷孙组件传值

    1. 父组件向子组件传值 步骤: 父组件中引入子组件、调用子组件并引用、在引用的标签上给子组件传值。 定义pare...

  • react基础

    认识React 组件 Jsx的引用 插值符号 组件的数据状态 组件的样式 组件的事件 组件的生命周期 React的...

  • Props、State

    Props属性实现组件传值 Props是properties的简写。通过Props可以组件传值如下例子 React...

  • Vue和React组件通信的总结

    在现代的三大框架中,其中两个Vue和React框架,组件间传值方式有哪些? 组件间的传值方式 组件的传值场景无外乎...

  • Vue中this.$emit()【子组件向父组件传值】的使用

    简述 this.$emit()的作用:子组件向父组件传值注:必须在父组件中引用子组件,然后实现传值 使用 子组件中...

  • react 父子组件传值(兄弟传值)

    react中父子组件传值 父向子: 父组件通过自定义属性向子组件传值,子组件通过this.props.属性名接收子...

  • React入门(二)组件

    本节知识点 (1)React组件化 (2)React父子组件传值 (一)组件 在React中组件都是对应的一个个类...

网友评论

      本文标题:react组件引用传值

      本文链接:https://www.haomeiwen.com/subject/wpugvktx.html