나는 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")
);
그러나 내 구성 요소에 로그인하려고하면 값이 정의되지 않습니다.
const Component = () => {
const staticLesson = useSelector((state: RootState) => state);
const dispatch = useDispatch();
console.log("counter", staticLesson.counter); <-- logs undefined
return (
...
)
}
내가 이해하지 못하는 것은 그냥 로그 staticLesson
하면 내 콘솔에 예상대로 나타나는 이유 입니다. 하지만 0
카운터 값 을 가져 오는 대신 개체의 값에 액세스하려고 하면 undefined
. staticLesson
내 devTools에 표시되고 있습니다. 거기에 있습니다 ... 난 당황 스럽습니다. 무엇을 엉망으로 만들고 있습니까?
나는 당신이 staticLesson
당신의 useSelector
전화로 주에서 재산을 반환해야한다고 확신합니다 . 에서 useSelector
당신은 반환하고 전체 상태를. 따라서 다음과 같아야합니다.
const staticLesson = useSelector((state: RootState) => state.staticLesson);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다