【问题标题】:how to send redux store to the client when routing in redux在redux中路由时如何将redux store发送到客户端
【发布时间】:2020-11-04 20:45:27
【问题描述】:

所以我是 nextjs 的新手,我无法在路由后在新构建的页面上保持调度状态。请问我们如何保持状态。

基本上我有一个侧边栏,我试图保持活动按钮的状态。 我尝试使用 getInitialProps 获取 url 查询和 useEffect 在加载时分发操作,但 redux 存储仍然是空的。

使用效果

useEffect(() => {
        var index;
        if (product_id === 'lime') index = 0
        if(product_id === 'travetine') index = 1
        // console.log(entry, index)
        entryClickButton({           // the action I am trying to dispatch however its not happening 
            type: entry,
            newEntries: entry === 'new-entries' ? index : undefined,
            soldEntries: entry === 'sold-entries' ? index : undefined
        })
    }, []);

【问题讨论】:

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


    【解决方案1】:

    您应该使用调度程序来调度您的操作

    actions.js:

    export const setEntries = (newEntries, soldEntries) => ({
        type: 'ENTRY_ACTION_NAME',
        newEntries,
        soldEntries
    })
    
    

    你的组件:

    import {connect} from 'react-redux';
    import {setEntries} from './actions';
    
    const YourComponent = ({setEntries}) => {
       /** your stuff here */
       useEffect(() => {
            var index;
            if (product_id === 'lime') index = 0
            if(product_id === 'travetine') index = 1
            // console.log(entry, index)
            setEntries(entry === 'new-entries' ? index : undefined,entry === 'sold-entries' ? index : undefined);
        }, []);
    /**  rest of your stuff here */
    }
    
    export default connect(state => ({
      // state to props mappings here
    }), dispatch => ({
      setEntries: (newEntries, soldEntries) => dispatch(setEntries(newEntries, soldEntries))
    }))(YourComponent);
    
    

    然后在你的减速器中的某个地方处理“ENTRY_ACTION_NAME”动作。

    【讨论】:

    • 基本上问题是我在将动作导入文件后没有将动作作为道具传递,非常感谢您的见解+1
    猜你喜欢
    • 2017-05-28
    • 2019-09-11
    • 2018-10-03
    • 2018-02-01
    • 2019-01-23
    • 2023-03-26
    • 1970-01-01
    • 2018-04-02
    • 2016-08-10
    相关资源
    最近更新 更多