Reactjs 《盖茨比使用调度》中的React Redux挂钩;无效的钩子调用";

Reactjs 《盖茨比使用调度》中的React Redux挂钩;无效的钩子调用";,reactjs,redux,react-redux,react-hooks,gatsby,Reactjs,Redux,React Redux,React Hooks,Gatsby,我正试图在我的盖茨比项目中使用Redux。我以前使用过Redux,但以前从未使用过挂钩。我认为这可能是Gatsby的问题,但我已经能够使用useStaticQuery挂钩(如下面的builder.js所示)。我已经检查了所有版本(见下文)和规则 以下是我收到的错误: 版本: 纱线列表 react@16.12.0 纱线列表反应重做 react-redux@7.1.3 纱线列表重做 redux@4.0.4 这是我的builder.js代码 import {graphql, useStatic

我正试图在我的盖茨比项目中使用Redux。我以前使用过Redux,但以前从未使用过挂钩。我认为这可能是Gatsby的问题,但我已经能够使用useStaticQuery挂钩(如下面的builder.js所示)。我已经检查了所有版本(见下文)和规则

以下是我收到的错误:

版本:

纱线列表

react@16.12.0
纱线列表反应重做

react-redux@7.1.3
纱线列表重做

redux@4.0.4
这是我的builder.js代码

import {graphql, useStaticQuery} from 'gatsby'
import React from "react"
import Armor from './armor'

export default () => {

  const data = useStaticQuery(graphql`
    query Data {
      allArmor(filter: {type: {eq: "chest"}}) {
        totalCount
        nodes {
          name
          attack
          magic
        }
      }
    }`)

  return (
    <div>
      <h1>{data.nodes[0]}</h1>
      <Armor/>
    </div>
  )
}
gatsby-ssr.js

import wrapWithProvider from './wrap-with-provider'
export const wrapRootElement = wrapWithProvider
wrap-with-provider.js

import React from 'react'
import { createStore, combineReducers } from 'redux'
import { Provider } from 'react-redux'

import * as reducers from './src/redux/reducers'

const initialState = {
  setCount: 0,
  setTotal: 0,
}

const combinedReducers = combineReducers({ ...reducers })
export default ({ element }) => {
  const store = createStore(combinedReducers, initialState)
  return <Provider store={store}>{element}</Provider>
}
使用redux“就像过去一样”不会开箱即用

您需要,即设置
gatsby browswer.js
gatsby ssr.js

要在Gatsby站点中使用redux,您需要连接到Gatsby的两个扩展点

一次在盖茨比的服务器呈现过程中运行的
wraprotement
中,一次在盖茨比的浏览器API的一部分
wraprotement

查看
/gatsby ssr.js
和.code>/gatsby browser.js以了解在本例中如何实现这一点


这是因为使用CRA引导时使用的入口点与Gatsby中的入口点不同。

您需要使用react组件再次包装包装,如下所示

const FireBaseApp=()=>{
const[loggedIn,setLoggedIn]=useState(/*一些代码*/)
useffect(()=>{
firebase.auth().onAuthStateChanged(用户=>{
setLoggedIn(!!用户)
})
})
返回(
{element}
)
}
常量包装器=({element})=>{
返回
}
exports.wraprotement=包装器
资料来源:

好吧,我没有提到,但让我把它添加到我的原始问题中,因为我确实有那个代码。抱歉。如果是这样,请尝试将安装程序中的示例上载到沙盒:我已将redux和react-redux添加到package.json中,但当它生成时,我只得到-./src/components/builder.js模块未找到:错误:无法解决“/sandbox/src/components”中的“react-redux”我看不出任何问题,你没有像我在回答中提到的那样配置,我花了几个小时试图让它工作。我按照你的方式配置了它。它在盖茨比沙箱中起作用,只是不在本地起作用。一定是我错过了一些简单的东西。我最终只是使用redux示例进行了分叉,这很有效。
import wrapWithProvider from './wrap-with-provider'
export const wrapRootElement = wrapWithProvider
import React from 'react'
import { createStore, combineReducers } from 'redux'
import { Provider } from 'react-redux'

import * as reducers from './src/redux/reducers'

const initialState = {
  setCount: 0,
  setTotal: 0,
}

const combinedReducers = combineReducers({ ...reducers })
export default ({ element }) => {
  const store = createStore(combinedReducers, initialState)
  return <Provider store={store}>{element}</Provider>
}
const setCount = (state = 0, action) => {
  switch (action.type) {
    case `UPDATE`:
      return action.count

    default:
      return state
  }
}