试图用 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>
</>
);
};