简体   繁体   English

React 上下文不在组件之间共享

[英]React context not shared between components

I use a react context between 2 components, however when I set the value this context in the child filtersBar component, it does not update or useffect in the second child map component.我在两个组件之间使用反应上下文,但是当我在子 filtersBar 组件中设置此上下文的值时,它不会在第二个子组件 map 中更新或使用效果。

contextprovider上下文提供者

const SearchContext = createContext();

const SearchContextProvider = ({children}) => {
const [contextValue, setContextValue] = useState(null);            
    return (
    <SearchContext.Provider value={[contextValue, setContextValue]}>
        {children}
    </SearchContext.Provider>
);

index.js索引.js

 <SearchContextProvider>
    <FiltersBar/>
      <Map/>
    </SearchContextProvider>

Filtersbar.js过滤器栏.js

const FiltersBar = () => {
        const [searchContext,setSearchContext] = useContext(SearchContext);
        const [searchLocationResult, setsearchLocationResult] = useState(null);
        const [inputSearchLocation, setinputSearchLocation] = useState("");
  
 useEffect(() => {
// stuff
                 searchContext.autocompleteLocation = [46.6, 8.5]
              setSearchContext(searchContext)

        }, [searchLocationResult, inputSearchLocation]);

Map.js Map.js

const Map = () => {
  
    const [searchContext, setSearchContext] = useContext(SearchContext);


    useEffect(() => {
      console.log("use effect map"+JSON.stringify(searchContext))
    }, [searchContext]);

I never see this use effect map console log message.我从来没有看到这个使用效果 map 控制台日志消息。 What am I missing?我错过了什么?

If the code you've shown is an accurate representation of your real code, the problem is that you're mutating state, not creating a new one.如果您显示的代码是真实代码的准确表示,那么问题是您正在改变 state,而不是创建新代码。 When you set state, react does a === between the state before and the state after.当您设置 state 时,React 在之前的 state 和之后的 state 之间执行=== It sees that they're the same object, so it thinks nothing has changed, and it does nothing.它看到它们是相同的 object,所以它认为什么都没有改变,它什么也不做。

So instead of this:所以不是这个:

searchContext.autocompleteLocation = [46.6, 8.5]
setSearchContext(searchContext)

Do this:做这个:

const newState = {
  ...searchContext,
  autocompleteLocation: [46.6, 8.5]
}
setSearchContext(newState);

Because of this因为这

useEffect(() => {
  /// stuff
  setSearchContext(searchContext) // ----> here
}, [searchLocationResult, inputSearchLocation]);

searchContext never changes. searchContext永远不会改变。 it always null, you awlays set null to setSearchContext function. Maybe you only get first console.log like use effect mapnull when reload or component init它总是null,你awlays设置nullsetSearchContext function。也许你只得到第一个console.log,比如在重新加载或组件初始化时use effect mapnull

React hook is a bit different with React state in Class component. React hook 与 Class 组件中的 React state 有点不同。 React hook consider state changes only if it's new object, u should create new object when you update the state. React hook 考虑 state 只有当它是新的 object 时才会发生变化,你应该在更新 state 时创建新的 object。

You can update the useEffect of FiltersBar as below: Filtersbar.js您可以更新 FiltersBar 的 useEffect 如下: Filtersbar.js

const FiltersBar = () => {
        ...
 useEffect(() => {
// stuff
    setSearchContext({...searchContext, autocompleteLocation: [46.6, 8.5]})

}, [searchLocationResult, inputSearchLocation]);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM