Reactjs React Redux钩子未找到状态
我正在关注这个 我的状态未定义,我连接了我的商店,或者我认为我连接了。我不知道是什么问题。减速器代码不相等。我只是想把任何东西都退回去,以得到一个美好的世界。我以前用过react reduxReactjs React Redux钩子未找到状态,reactjs,redux,react-hooks,Reactjs,Redux,React Hooks,我正在关注这个 我的状态未定义,我连接了我的商店,或者我认为我连接了。我不知道是什么问题。减速器代码不相等。我只是想把任何东西都退回去,以得到一个美好的世界。我以前用过react redux //index.js import React from 'react'; import ReactDOM from 'react-dom'; import App from './js/App'; import * as serviceWorker from './serviceWorker'; im
//index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './js/App';
import * as serviceWorker from './serviceWorker';
import { Provider } from 'react-redux';
import { createStore,} from 'redux';
import reducers from './js/reducers';
const store = createStore(reducers);
ReactDOM.render(
<Provider store={store}>
<React.StrictMode>
<App />
</React.StrictMode>
</Provider>,
document.getElementById('root')
);
您缺少一个的初始状态
import { EXAMPLES } from "../actions/types";
// declare it here.
const initialState = {
data:[]
};
export function OneReducer(state = initialState, action) {
switch (action.type) {
case EXAMPLES:
return action.payload;
default:
return state;
}
return state;
}
你可以试试这个
const data = useSelector((state) => state.data) // refers to initial state data property
感谢我的工作,我发现有人发布了有效的代码,我将其与我的代码进行了比较,它是“(state)=>state.data”我只是使用“state”
//ExampleReducers.js
import {EXAMPLES} from "../actions/types";
export function OneReducer (state = {}, action) {
switch (action.type) {
case EXAMPLES:
return action.payload
default:
return state
}
return state;
}
import { EXAMPLES } from "../actions/types";
// declare it here.
const initialState = {
data:[]
};
export function OneReducer(state = initialState, action) {
switch (action.type) {
case EXAMPLES:
return action.payload;
default:
return state;
}
return state;
}
const data = useSelector((state) => state.data) // refers to initial state data property