已尝试使用next-redux-wrapper,redux,Next.js进行计数。
我观察到,当我点击一些计数器按钮,然后移动到其他页面并返回计数器页面时,getServerSideProps
再次将计数器初始化为3。我理解这个库,因为它有助于将SSR期间的分派结果合并到客户端redux存储,但不会将客户端存储状态同步到服务器redux存储。
我的理解正确吗?
这是我的计数器应用程序的代码
index.tsx
export const getServerSideProps = wrapper.getServerSideProps(
(store) => async () => {
store.dispatch(add(3));
return {
props: {},
};
}
);
const index = (props: any) => {
const count = useSelector((state: AppState) => state.counter.count);
const dispatch = useDispatch();
return (
<>
<Link href='/book'>// move to other page then come back
<a>move</a>
</Link>
<div>{count}</div>
<button onClick={() => dispatch(add(1))}>+</button>
<button onClick={() => dispatch(deleter(2))}>-</button>
</>
);
};
发布于 2021-09-26 09:18:43
它将帮助您在客户端对来自服务器的数据进行消重处理,但是由于SSG呈现的数据可能是在请求的前几天创建的,甚至连SSR都不知道客户端上发生了什么,因此无法将数据从客户端同步到服务器。
https://stackoverflow.com/questions/69325929
复制相似问题