gpt4 book ai didi

React component not updated with new Redux state(Reaction组件未使用新的Redux状态更新)

转载 作者:bug小助手 更新时间:2023-10-28 13:32:33 26 4
gpt4 key购买 nike



My problem is that a component doesn't seem to receive the proper redux state (in time?) and therefore doesn't display the sidebar component in this redux state. The called page looks like:

我的问题是组件似乎没有接收到正确的复制状态(在时间上?)并且因此不显示处于该重复状态的侧边栏组件。调用的页面如下所示:


const MyLandingPage: NextPage = () => {
const { setLayout } = useSetLayout(); // the Redux dispatch method, see below
...
useEffect(
() => {
setLayout({
sidebar: {
content: <FeedSidebar filters={filters} setFilters={setNewFilters} />
});
},
[...]
);
return <MyComponent ... />
}

This page will be embedded in the side column of my Layout.tsx:

此页面将嵌入到My Layout.tsx的侧栏中:


export const Layout: React.FC = () => {
const sidebar = useSidebarContentSelector(); // the Redux selection method, see below
console.log('sidebar in Layout:', sidebar);
return <SomeOtherComponents>{sidebar?.content}<SomeOtherComponents>;
}

The Redux-related hooks for the sidebar state looks like this:

侧边栏状态的Redux相关挂钩如下所示:


export const useSidebarContentSelector = () => useSelector((state: RootState) => state.sidebarContent);

export const useSetLayout = () => {
const { setSidebar } = sidebarContentSlice.actions;
const dispatch = useAppDispatch();
return {
setLayout: (commands: { sidebar?: SidebarState) => {
console.log('setting sidebar:', commands.sidebar);
dispatch(setSidebar(commands.sidebar));
}
}
}

The logs reads like:

日志如下所示:


sidebar in Layout: null
setting sidebar: Object { content: {…} }
sidebar in Layout: null

useSidebarContentSelector and useSidebarContentDispatch work on other occasions, I'm rather sure they are alright. But for some reason the sidebar in Layout.tsx doesn't receive the update and I figure just keeps displaying the state from the SSR.

useSidebarContentList和useSidebarContentDispatch在其他场合也可以工作,我相当肯定它们是好的。但是由于某些原因,Layout.tsx中的侧边栏没有收到更新,我想只是继续显示来自SSR的状态。


I'm grateful for any ideas why the updated sidebar isn't received dispite being dispatched and the Layout component rerendering!

我非常感谢任何想法,为什么没有收到更新的侧边栏,尽管被调度和布局组件重新呈现!


更多回答
优秀答案推荐

Found it. I wasn't using a Redux store singleton, so with every rerender a new store was created. One solution might be to create the store with const store => useMemo(() => configureStore(...), []), but for my design it was more straight-forward to go with

找到它了。我没有使用Redux商店单件,所以每个租户都会创建一个新的商店。一种解决方案可能是使用const store=>useMemo(()=>figureStore(...),[])创建存储,但对于我的设计来说,使用它更简单


const isServerSide = typeof window === 'undefined';

const clientSideReduxStore = configureStore(...);

export const ReduxProvider: React.FC<
Omit<ProviderProps, 'store'> & { preloadedState?: Partial<RootState> }
> = (props) => {
return isServerSide ? Provider({store: configureStore(...)) : clientSideReduxStore
}


Note that the on SSR a new store must be created at any call, so I mustn't return clientSideReduxStore in case I'm at the server-side.

请注意,在SSR上,必须在任何调用时创建一个新存储,因此我不能返回clientSideReduxStore,以防我在服务器端。


更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com