组件之间如何通信(react-route下面的)

#1

一般的组件通信大概知道,在两个组件的父组件上声明一个公共方法,改变state。

但是,在reat-router下,貌似并不能设置state,那么怎么通信呢

看下图,截了部分代码

#2

redux?

#3

children可以拿来重新包装的,不是一定要写成{this.props.children}这样在render里。
可以用React.cloneElement给children加props

#4

是把this.props.children封成一个组件吧,在哪能获取到这个this.props.chidren,我是在Navs的render里面声明了一个函数,然后在Navs的render的renturn里用了。
是这个意思吗,那怎么获取到NavsChild上的aa

render(){
        //上面是其他代码

        const NavsChild=()=>{
            return (
                <div className="container">
                    {React.cloneElement(this.props.children)}
                </div>
            )
        }
        return (
            <div>
                {this.state.localUser? <Login/>: <NoLogin/>}
                <NavsChild aa="ssbb"/>
            </div>
        )
    }
#5
this.props.children.map(function(child){
    var props={aa:"ssbb",func:function(){}};
    return React.cloneElement(child,props);
})
1 Like
#6

我的this.props.children不是个数组,很奇怪,仿照你的方法,不遍历,直接return,就可以了,感谢

{React.cloneElement(this.props.children,{aa:'sssbbb'})}