如何在Gatsby中安装Redux

黄 :

我担心如何在gatsby中使用redux。我已经安装了redux:

yarn add redux react-redux

这对我不起作用,我不知道该怎么做。任何帮助将不胜感激,谢谢!

BloodyLogic:

Gatsby与插件有关,在我看来您尚未安装gatsby redux插件,请先安装gatsby插件:

npm install --save gatsby-plugin-react-redux react-redux redux

// Yarn
yarn add gatsby-plugin-react-redux react-redux redux

如何使用

创建一个文件createStore.tssrc/state/./src/state/createStore.ts

与gatsby-config中提供的路径相同

  import { createStore } from 'redux';
    
    function reducer() {
      //...
    }
    
    // preloadedState will be passed in by the plugin
    export default preloadedState => {
      return createStore(reducer, preloadedState);
    };
    ./gatsby-config.js
    
    module.exports = {
      plugins: [
        {
          resolve: `gatsby-plugin-react-redux`,
          options: {
            // [required] - path to your createStore module
            pathToCreateStoreModule: './src/state/createStore',
            // [optional] - options passed to `serialize-javascript`
            // info: https://github.com/yahoo/serialize-javascript#options
            // will be merged with these defaults:
            serialize: {
              space: 0,
              isJSON: true,
              unsafe: false,
            },
            // [optional] - if true will clean up after itself on the client, default:
            cleanupOnClient: true,
            // [optional] - name of key on `window` where serialized state will be stored, default:
            windowKey: '__PRELOADED_STATE__',
          },
        },
      ],
    };

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章