Consumer from Context API inside Dialog Component cannot get Provider value
See original GitHub issueI have a Button component which is wrapped by a Consumer. And I put this button inside Dialog. Whenever the Provider value changes, the button is always rendered with the initial context value instead of the latest Provider value
Example: Say I have the following ThemeProvider and I place it on the root Component
const {Provider, Consumer} = React.createContext({
color: "#FFFFFF",
setTheme: ()=>{},
});
const withTheme = Component => props => <Consumer>{value => { return <Component {...props} theme={value}/>}}</Consumer>;
class ThemeProvider extends React.Component {
constructor(props) {
super(props);
this.state = {
theme: {
color: "#FFFFFF",
setTheme: this.setTheme,
}
}
}
setTheme = () => {
this.setState(({theme}) => ({
theme: {
...theme,
color: '#000000'
}
}))
}
};
render() {
return (
<Provider value={this.state.theme}>
{this.props.children}
</Provider>
)
}
}
export {withTheme, ThemeProvider}
And I have a Button
const PrimaryButton = (props) => {
return (
<TouchableHighlight style={{
backgroundColor: props.theme.color
}}>
<Text>
{props.title}
</Text>}
</TouchableHighlight>)
};
export default withTheme(PrimaryButton)
If I put the PrimaryButton inside Dialog Component, after I have run setTheme(), the background color of the PrimaryButton inside Dialog Component is still “#FFFFFF” when the dialog pops up.
Version: “react”: “16.8.3”, “react-native”: “0.59.8”, “react-native-popup-dialog”: “^0.18.3”
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:9
Top Results From Across the Web
reactjs - Context.Consumer cannot get value from Provider
@DragoşPaulMarinescu This is just for testing so I set the default value it the context file as isOpen: true and set it in...
Read more >What is Context API and useContext? Everything You Need to ...
The Provider component receives a prop called “value,”, which can be accessed from all the components wrapped inside the Provider. <AuthContext.
Read more >The Guide to Learning React Hooks (Examples & Tutorials)
Learn all about React Hooks with this hands-on guide. Includes tutorials and code examples on using hooks for state and effects, for context...
Read more >Input Components - React-admin - Marmelab
Input components must be used inside a Form element (e.g. <Form> , <SimpleForm> ... Tip: defaultValue cannot use a function as value.
Read more >Building an expressive API for custom confirm dialogs in React
Building a simple Confirm Dialog component is easy in React. ... We can create a context provider that would manage the rendering of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
We also encounter this, but with
react-navigation-hooks
and using the hooks inreact-redux
.We have the same problem. It breaks apollo completely …