简体   繁体   English

React Native如何在生命周期中监听上下文变化

[英]React native how to listen context change in lifecycle

Overview 总览

Use React.createContext() replace props , but also want to know what changed in child component.So I use lifecycle componentWillReceiveProps or the other who has nextContext argument. 使用React.createContext()替换props ,但是还想知道子组件中发生了什么变化,因此我使用生命周期componentWillReceiveProps或其他具有nextContext参数的组件。

And now, when I setState value and console nextContext or this.context ,but get nothing {} 而现在,当我setState值和控制台nextContextthis.context ,但什么也没得到{}

Code

Provider 提供者

//I create a context to transmit data
const {Provider,Consumer} = React.createContext({visible:true});

export default class extends Component {
    constructor(props){
        super(props);
        this.state={
            visible:false,
        };
    }

    toggleMenus=()=>{
        let visible = this.state.visible;
        this.setState({visible:!visible})
    };



    render(){
        return (
            <Provider value={{visible:this.state.visible}}>
                <View style={{flex:1}}
                      onStartShouldSetResponder={this.toggleMenus}
                >
                    <Menus />
                </View>
            </Provider>
        )
    }
}

Consumer 消费者

class Menus extends React.Component {
    componentWillReceiveProps(nextProps, nextContext) {
        console.log(nextProps, nextContext, this.context)//{},{},{}
        //I cant get nextContext to listen context change
    }

    shouldComponentUpdate(nextProps, nextState, nextContext) {
        return false //UI still update
    }

    render(){
        return (
            <Consumer>
                {({visible})=>
                    <Text>{visible?1:2}</Text>
                }
            </Consumer>
        )
    }
}

🎉I solved it. solved我解决了。

class need manual assign ContextObject contextType 类需要手动分配ContextObject contextType

Reason 原因

reference https://reactjs.org/docs/context.html#classcontexttype 参考https://reactjs.org/docs/context.html#classcontexttype

The contextType property on a class can be assigned a Context object created by React.createContext() . 可以为类的contextType属性分配一个React.createContext()创建的Context对象。 This lets you consume the nearest current value of that Context type using this.context . 这使您可以使用this.context消耗该Context类型的最接近的当前值。 You can reference this in any of the lifecycle methods including the render function. 您可以在任何生命周期方法(包括render函数)中引用此方法。

Code

commit 承诺

- const {Provider,Consumer} = React.createContext({visible:true});
+ const MenusContext = React.createContext({visible:true});

- <Provider value={{visible:this.state.visible}}>
+ <MenusContext.Provider value={{visible:this.state.visible}}>

- <Consumer>
+ <MenusContext.Consumer>

Consumer 消费者

Menus.contextType = MenusContext;

⚠️Note ⚠️注意

shouldComponentUpdate cant listen context change only, so when context change ,UI will change as well even if return false in shouldComponentUpdate life. shouldComponentUpdate不能仅侦听上下文更改,因此,当上下文更改时,即使在shouldComponentUpdate生命周期中return false ,UI也将更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM