Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Redux:useSelector值未定义,尽管它出现在devTools中_Javascript_Reactjs_Redux_React Redux - Fatal编程技术网

Javascript Redux:useSelector值未定义,尽管它出现在devTools中

Javascript Redux:useSelector值未定义,尽管它出现在devTools中,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,我正试图建立一个redux环境,但我不明白为什么会有这种行为 我的减速机上有我的根状态: export type RootState = { counter: number; }; const initialState: RootState = { counter: 0 }; const staticLesson = (state = initialState, action: any) => { switch (action.type) { case "INC

我正试图建立一个redux环境,但我不明白为什么会有这种行为

我的减速机上有我的根状态:

export type RootState = {
  counter: number;
};
const initialState: RootState = { counter: 0 };

const staticLesson = (state = initialState, action: any) => {
  switch (action.type) {
    case "INCREMENT":
      return state.counter + 1;
    default:
      return state;
  }
};

export default staticLesson;
import staticLesson from "./staticLesson/index";
import { combineReducers } from "redux";

const rootReducer = combineReducers({
  staticLesson,
});

export default rootReducer;
import { createStore } from "redux";
import rootReducer from "./redux/reducers";
import { Provider } from "react-redux";
import { composeWithDevTools } from "redux-devtools-extension";

const store = createStore(rootReducer, composeWithDevTools());

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById("root")
);
然后我将其合并到这里:

export type RootState = {
  counter: number;
};
const initialState: RootState = { counter: 0 };

const staticLesson = (state = initialState, action: any) => {
  switch (action.type) {
    case "INCREMENT":
      return state.counter + 1;
    default:
      return state;
  }
};

export default staticLesson;
import staticLesson from "./staticLesson/index";
import { combineReducers } from "redux";

const rootReducer = combineReducers({
  staticLesson,
});

export default rootReducer;
import { createStore } from "redux";
import rootReducer from "./redux/reducers";
import { Provider } from "react-redux";
import { composeWithDevTools } from "redux-devtools-extension";

const store = createStore(rootReducer, composeWithDevTools());

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById("root")
);
我使用提供商包装我的应用程序:

export type RootState = {
  counter: number;
};
const initialState: RootState = { counter: 0 };

const staticLesson = (state = initialState, action: any) => {
  switch (action.type) {
    case "INCREMENT":
      return state.counter + 1;
    default:
      return state;
  }
};

export default staticLesson;
import staticLesson from "./staticLesson/index";
import { combineReducers } from "redux";

const rootReducer = combineReducers({
  staticLesson,
});

export default rootReducer;
import { createStore } from "redux";
import rootReducer from "./redux/reducers";
import { Provider } from "react-redux";
import { composeWithDevTools } from "redux-devtools-extension";

const store = createStore(rootReducer, composeWithDevTools());

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById("root")
);
从“redux”导入{createStore};
从“/redux/reducers”导入rootReducer;
从“react redux”导入{Provider};
从“redux devtools扩展”导入{composeWithDevTools};
const store=createStore(rootReducer,composeWithDevTools());
ReactDOM.render(
,
document.getElementById(“根”)
);
但是,当我尝试将其记录到组件中时,该值未定义。

const Component = () => {
  const staticLesson = useSelector((state: RootState) => state);   
  const dispatch = useDispatch();

  console.log("counter", staticLesson.counter); <-- logs undefined

  return (
    ...
  )
}
const组件=()=>{
const staticlessource=useSelector((状态:RootState)=>state);
const dispatch=usedpatch();

console.log(“counter”,staticlessor.counter);我很确定您应该从
useSelector
调用中的状态返回
staticlessor
属性。在
useSelector
中,您将返回整个状态。因此它应该是:

const staticLesson = useSelector((state: RootState) => state.staticLesson);

亲爱的,谢谢你这么做了。错过这么一件小事