我担心如何在gatsby中使用redux。我已经安装了redux:
yarn add redux react-redux
这对我不起作用,我不知道该怎么做。任何帮助将不胜感激,谢谢!
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.ts
中src/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] 删除。
我来说两句