简体   繁体   中英

'react-redux` module not found with React Toolkit

I got a React-app project in which I installed npm and Redux Toolkit .

After having added a store provider for the whole app in this way

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';

import './index.css';
import App from './App';
import store from './store/index';

ReactDOM.render(<Provider store={store}><App /></Provider>, document.getElementById('root'));

if I npm start the app I get this error:

Module not found: Can't resolve 'react-redux' in '/Desktop/React/redux-app/src'

I've read on the official Redux site :

Redux Toolkit includes the Redux core, as well as other key packages we feel are essential for building Redux applications

So I guess that having Redux Toolkit alone should be enough to resolve any react-redux module.

I noticed that my package.json and package-lock.json are in the directory from which I run npm start , that is /redux-app while the module is not found in /redux-app/src . Is that fine? Should I move the .json files? How can I fix the problem in general?

React-Redux isn't part of the Redux core.

Redux Toolkit includes the Redux core, as well as other key packages we feel are essential for building Redux applications

Redux isn't React specific, it works with whatever stack you want to try it with. When working with Redux and React, you will want to install and use React-Redux to get it working better with React. You don't necessarily need to, but it helps a lot.

In this case, it's focusing on applications that use redux. It's a toolkit that drastically improves the use of redux in particular. It doesn't care what you try and use redux with, so it won't affect the binding logic between react and redux (the react-redux package).

A good test for this is if there's a different base package name you are trying to pull the code in from, you should install that other package (even if it appears to work). ie import {} from 'react-redux' vs import {} from '@reduxjs/toolkit .

In answer to your question, you'll need to run npm install react-redux from your base directory ( /redux-app ). You shouldn't move your .json files around from where they are. They are meant to be in the root of your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM