提问者:小点点

如何在盖茨比中安装Redux


我担心我怎么能在盖茨比中使用redux。 我已经安装了Redux:

yarn add redux react-redux

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


共3个答案

匿名用户

盖茨比是所有关于插件,似乎在我看来你还没有安装盖茨比redux插件,首先安装盖茨比插件:

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

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

src/state/中创建文件createstore.ts,类似于./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__',
          },
        },
      ],
    };

匿名用户

这个家伙非常清楚地解释了Redux和Gatsby,他还提供了repo链接,如果你在任何地方陷入困境,你也可以查看源代码,

只需克隆回购并开始操作:

盖茨比新盖茨比-还原-测试& cd Gatsby-Redux-Test

匿名用户

安装gastby插件以便在gastby中使用redux

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

并配置gatsby-config.js