【问题标题】:Did I correctly understand the behaviour of next-redux-wrapper?我是否正确理解了 next-redux-wrapper 的行为?
【发布时间】:2021-11-18 09:10:46
【问题描述】:

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

【问题讨论】:

    标签: reactjs redux next.js server-side-rendering next-redux-wrapper


    【解决方案1】:

    这将帮助您在客户端上从服务器上混合数据,但由于 SSG 渲染的数据可能在请求前几天创建,甚至 SSR 也不知道客户端上发生了什么,因此无法同步从客户端到服务器的数据。

    【讨论】:

      猜你喜欢
      • 2021-10-05
      • 2016-05-03
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 2020-09-30
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      相关资源
      最近更新 更多