question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consumer from Context API inside Dialog Component cannot get Provider value

See original GitHub issue

I 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:open
  • Created 4 years ago
  • Reactions:3
  • Comments:9

github_iconTop GitHub Comments

1reaction
CoenWarmercommented, Dec 5, 2019

We also encounter this, but with react-navigation-hooks and using the hooks in react-redux.

1reaction
beatleczcommented, Sep 7, 2019

We have the same problem. It breaks apollo completely …

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found