I need to understand that how the consumers of a particular provider knows that the value has been changed and the consumers need re-rendering? The documentation says that the algorithm detects the value changes using Object.is()
and I do not quite understand why we ever need to detect changes in the consumers, since the ONLY way we can change the context value is updating the state in the parent component, which always triggers re-renders in every child components of the parent component and we always get fresh context data.
Or the consumers check for changed values because is there any mechanism to provide the changed value to the consumers without using the state in the parent component at all?
class App extends Component {
state = {
userState: {
userName: "yasmikash",
userId: "u8662",
signedIn: true,
},
};
handleOnChange = () => {
this.setState({
userState: {
...this.state.userState,
signedIn: !this.state.userState.signedIn,
},
});
};
render() {
return (
<>
<h1>App</h1>
<button onClick={this.handleOnChange}>Toggle Signed In</button>
<AuthContext.Provider value={this.state.userState}>
{/* <Component1 > */}
{/* <Component2 > */}
<Profile />
{/* </Component2> */}
{/* </Component1 > */}
</AuthContext.Provider>
</>
);
}
}