gpt4 book ai didi

javascript - UseSelector 状态未定义

转载 作者:行者123 更新时间:2023-12-03 07:06:17 27 4
gpt4 key购买 nike

页面不渲染,引用 TypeError: state is undefined ,回溯到 中的这一行SelectForm.js : const filter = useSelector(state => state.filter); .

我花了几个小时试图弄清楚我做错了什么。我已经尝试过 createSelector 但没有奏效。我尝试发送“获取初始状态”操作,但没有成功。该组件被包装在提供者标签中。我不确定为什么我无法访问该州。在这一点上,我看不到任何我一直在寻找它的缺陷。

代码片段

reducer .js

let initialState = {
filter: {
country: null,
state: null,
level: null,
team: null
},

isDBConnecting: false,
isDBConnected: false,
isDBError: false
}

const SelectorReducer = (state=initialState, action) => {
switch (action.type) {
case 'DB_CONNECT_INIT':
return {
...state,
isDBConnecting: true,
isDBConnected: false,
isDBError: false,
};
...
...
}

export default SelectorReducer;

Action .js
export const initializeDBConnection = () => {
return {
type: 'DB_CONNECT_INIT'
}
};

父组件.js
import React from 'react';
import { createStore } from 'redux';
import { Provider } from 'react-redux'; //import provider to provide component access to the state

//Component imports
import SelectForm from './components/SelectForm'
import SelectorReducer from '.../reducer.js'

const SelectorStore = createStore(SelectorReducer);

const ParentComponent = () => {

return (
<div className="page-container">
<div id="carousel">
<div id="wrapper">
<Provider store={SelectorStore}>
<SelectForm />
</Provider>
</div>
</div>
</div>
)
}

SelectForm.js(子组件,包裹在上面的 Provider 标签中)
//IMPORTS
import React from 'react'; //import react
import { useSelector, useDispatch } from 'react-redux';

//COMPONENT IMPORTS
import FormGroup from '../FormGroup';
import { * as actions } from '.../actions.js';

const SelectForm = (props) => {

//STATEFUL IMPORTS
//filter
const filter = useSelector(state => state.filter);

最佳答案

感谢@NicholasTower 评论中的答案。我的 reducer 没有默认情况

default: return state

把它放进去解决了这个问题。

关于javascript - UseSelector 状态未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62111011/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com