简体   繁体   中英

cannot import store from redux library in react app

Im trying out the redux example on the official react-redux website HERE with the following javascript:

// index.js
import React from 'react'
import ReactDOM from 'react-dom'
import TodoApp from './TodoApp'

import { Provider } from 'react-redux'
import store from './redux/store'

const rootElement = document.getElementById('root')
ReactDOM.render(
  <Provider store={store}>
    <TodoApp />
  </Provider>,
  rootElement
)

But I get the following error:

Module not found: Can't resolve './redux/store' in '/mnt/d/Projects/engage-calculator/src'

my package.json is

{
  "name": "engage-calculator",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "bootstrap": "^4.3.1",
    "react": "^16.10.2",
    "react-dom": "^16.10.2",
    "react-redux": "^7.1.0",
    "react-scripts": "3.2.0",
    "redux": "^4.0.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

What is wrong?? The codesandbox page has the same dirextory structure as mine i also tried named imports by using {} but nothing changed. I can assure you that redux is installed i did npm install many times

directory structure:

.
├── README.md
├── package-lock.json
├── package.json
├── public
│   ├── favicon.ico
│   ├── index.html
│   ├── logo192.png
│   ├── logo512.png
│   ├── manifest.json
│   └── robots.txt
└── src
    ├── App.css
    ├── App.js
    ├── App.test.js
    ├── components
    │   └── calculator.js
    ├── index.css
    ├── index.js
    ├── logo.svg
    └── serviceWorker.js

your problem is that you should create your store, they used a custom path, so they created a folder called redux and a file called store file

- application
   - redux
     - store.js

so your path will be ./redux/store

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