Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

useSelector doesn't work in react redux app

I am creating a counter application using react and redux.

In the counterSlice.js file, I included this line:
export const selectCount = (state) => state.count;

And in the Counter.js file, I have included these lines:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import { changeValueBy, selectCount } from "./counterSlice";

const count = useSelector(selectCount);

But the value of count remains undefined.
My complete code is here.
Please tell me what I’m doing wrong.

>Solution :

You missed out to create a store and wrap the app with its Provider as following:

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;

BTW, I fixed your codesanbox here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading