简体   繁体   English

React context.provider不会更改默认值

[英]React context.provider doesn't change the default value

I'm using react context. 我正在使用反应上下文。 First I give to the context default value, and then I give to Context.Provider different value. 首先,我给上下文默认值,然后给Context.Provider不同的值。 but the consumer still get the default value 但消费者仍然获得默认值

HOC that pass notifications (for now is just a simple string) 传递通知的HOC(目前只是一个简单的字符串)

export const NotificationContext = React.createContext<string>('default');

function withNotifications<T>(WrappedComponent: React.ComponentType<T>) {

    return class ComponentWithTheme extends React.Component<T, { data: any }> {
        public render() {
            return (
                <NotificationContext.Provider value={'value'}>
                    <WrappedComponent {...this.props as T} />
                </NotificationContext.Provider>
            );
        }
    };
}

export default withNotifications;

Consumer 消费者


const Topbar: React.FC<TopbarProps> = props => {
    //const [isNotificationOpen, setIsNotificationOpen] = useState(false);
    //const [unreadCount, setUnreadCount] = useState(0);

    const notificationContext = useContext(NotificationContext);
    useEffect(() => {
        console.log(notificationContext);
    }, Object.values(notificationContext));

    console.log(notificationContext);


    return (
        <div>topBar</div>  );
};
export default Topbar;


I'm still get the "default" string and not the "value" 我仍然得到“默认”字符串而不是“值”

Your value to the provider should be an object, 您对提供者的价值应该是一个对象,

const value = {
 name: "kim"
}
 <NotificationContext.Provider value={value}>
  <WrappedComponent {...this.props as T} />
 </NotificationContext.Provider>

暂无
暂无

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

相关问题 在 React 中将多个值和设置器对传递给 Context.Provider - Passing multiple value and setter pairs to Context.Provider in React 为什么 React Context.Provider 是必需的(或有用的)? - Why Is React Context.Provider Necessary (Or useful)? React Native Context - 从渲染之外的 Context.provider 中检索值 function - React Native Context - retrieve the value from Context.provider outside of the render function React - value 属性是必需的<Context.Provider> . 你是拼错了还是忘记通过了? - React - The value prop is required for the <Context.Provider>. Did you misspell it or forget to pass it? 如何访问react Context.Provider值内的另一个函数? - How can I access another function inside the react Context.Provider value? 如何在 react 和 typescript 中使用 context.provider 设置 state? - How to set the state using context.provider in react and typescript? 挂钩更改 state 不会更新上下文提供程序的值? - hook change state doesn't update context provider's value? React Context使用提供程序不起作用 - React Context doesn't work using Provider 当提供程序组件父状态更改时,为什么无法更新子组件中的值(使用react上下文)? - Why can't update a value in a child component (with react context) when a provider component parent state change? 我正在尝试使用挂钩来管理Context.Provider的状态 - I am attempting to use hooks to manage the state of Context.Provider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM