I’m having some troubles at the time to execute my Visual Studio Code project on browser.
Error.
export 'default' (imported as 'Redux') was not found in 'react-redux' (possible exports: Provider, ReactReduxContext, batch, connect, createDispatchHook, createSelectorHook, createStoreHook, shallowEqual, useDispatch, useSelector, useStore)
My code it’s like this.
src/index.js
import ReactDOM from "react-dom/client";
import Redux from "react-redux";
import Store from "./store/index";
import App from "./App";
import "./index.css";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<Redux.Provider store={Store}>
<App />
</Redux.Provider>
);
src/store/index.js
const initialState = { counter: 0, visible: true };
const counterReducer = (state = initialState, action) => {
switch (action.type) {
case "increment":
return {
counter: state.counter + 1,
visible: state.visible,
};
case "decrement":
return {
counter: state.counter - 1,
visible: state.visible,
};
case "switchVisibility":
return {
counter: state.counter,
visible: !state.visible,
};
default:
return state;
}
};
const store = Redux.createStore(counterReducer);
export default store;
src/components/counter.js
import style from "./Counter.module.css";
const Counter = () => {
const distpach = Redux.useDispatch();
const counter = Redux.useSelector((state) => state.counter);
const visibility = Redux.useSelector((state) => state.visibility);
const incrementHandler = () => {
distpach({ type: "increment" });
};
const decrementHandler = () => {
distpach({ type: "decrement" });
};
const toggleCounterHandler = () => {
distpach({ type: "switchVisibility" });};
return (
<main className={style["counter"]}>
<h1>Redux Counter</h1>
{visibility && <div className={style["value"]}>{counter}</div>}
<div>
<button onClick={incrementHandler}>Increment</button>
<button onClick={decrementHandler}>Decrement</button>
</div>
<button onClick={toggleCounterHandler}>Toggle Counter</button>
</main>
);
};
export default Counter;
The exports looks right, I don’t know what happen.
I think the way I’m calling the function Redux.createStore in the file src/store/index.js, is obsolete, but it’s the reason of my error?
Somebody know the way to fix this problem?
2
Answers
How NAZIR HUSSAIN says, I had some issues in how I declared the imports and how I declared the export.
The next, works for me!
src/index.js
and my file... src/store/index.js
Thank you, a lot!
dont make it complicated by importing
Redux from 'react-redux'
,actually
react-redux
dont export default anything, so you can not import directly.on the other hand
react-redux
exportsProvider
,useDispatch
,useSelector
etc..so you should import like this
and the use these directly
you can understand it in your example code in this you
export default store
note that store with smalls
. and you are using Store with capitalS
here if you remove the
default
keyword then you need to import store as