useSelector在react redux应用程序中不起作用

nnvyjq4y  于 2022-11-12  发布在  React
关注(0)|答案(1)|浏览(205)

我正在使用react和redux创建计数器应用程序。
counterSlice.js文件中,我包括了下面这行:export const selectCount = (state) => state.count;
Counter.js文件中,我包含了以下行:
import { changeValueBy, selectCount } from "./counterSlice";
const count = useSelector(selectCount);
但是count的值仍然是undefined。我的完整代码是here。请告诉我我做错了什么。

pgx2nnw8

pgx2nnw81#

您错过了创建一个商店并将应用与其Provider Package 在一起的步骤,如下所示:

import { Counter } from "./features/counter/Counter";
import { Provider } from "react-redux";
import { createStore } from "redux";
import counter from "./features/counter/counterSlice";

const store = createStore(counter);

function App() {
  return (
    <Provider store={store}>
       <Counter />
    </Provider>
  );
}

export default App;

顺便说一句,我修正了你的代码sanbox here

相关问题