react-native-modals icon indicating copy to clipboard operation
react-native-modals copied to clipboard

Consumer from Context API inside Dialog Component cannot get Provider value

Open OscarYuen opened this issue 6 years ago • 9 comments

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"

OscarYuen avatar Aug 22 '19 17:08 OscarYuen

We have the same problem. It breaks apollo completely ...

beatlecz avatar Sep 07 '19 08:09 beatlecz

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

CoenWarmer avatar Dec 05 '19 10:12 CoenWarmer

also encounter with this. any fix?

OrLevy23 avatar Apr 06 '20 13:04 OrLevy23

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

react-native-modals using library react-native-root-siblings

  • Add react-native-root-siblings in your dependency by installing it.
  • Add lines in your App.js file where you provide redux store
import { setSiblingWrapper } from 'react-native-root-siblings';
import { Provider } from 'react-redux';

// const store = ... get store;

// Call this before using redux context inside RootSiblings.
setSiblingWrapper((sibling) => <Provider store={store}>{sibling}</Provider>);

ManinderGharuan avatar May 12 '20 10:05 ManinderGharuan

Having the same issue! Context values do not work with this modal.

Saad-Bashar avatar Sep 29 '20 06:09 Saad-Bashar

Any solution for this yet?

Saad-Bashar avatar Oct 05 '20 09:10 Saad-Bashar

Any solution for this yet?

I switched to community library. It doesn't have such an issue. https://github.com/react-native-community/react-native-modal

Only very fews changes are needed for migration

OscarYuen avatar Oct 06 '20 12:10 OscarYuen

@OscarYuen , I may need to do as well. The problem is I have used this package in a lot of places in my app :(

Saad-Bashar avatar Oct 06 '20 13:10 Saad-Bashar

@ManinderGharuan , I have been trying your solution and am unable to implement what you have said. Can you please help?

Saad-Bashar avatar Oct 14 '20 06:10 Saad-Bashar