简体   繁体   English

React redux 存储未连接

[英]React redux store isn't connecting

I'm connecting store to my react application, But it gives me error TypeError: state is undefined我正在将商店连接到我的反应应用程序,但它给了我错误 TypeError: state is undefined

store/index.js(Creating Reducer function) store/index.js(创建Reducer函数)

import {createStore} from 'redux';

const counterReducer = (state:{counter:0},action) => {
  if(action.type==='increment')
  {
    return {
      counter: state.counter + 1
    }
  }
   if (action.type === 'decrement') {
    return {
      counter: state.counter - 1
    }
  }
  return state;
}
const store = createStore(counterReducer);
export default store;

main index.js(Main index of my file) main index.js(我的文件的主索引)

import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux';
import store from './store/index';

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

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

Now I'm using my Counter components like this:(src/components/Counter.js)现在我正在使用我的 Counter 组件:(src/components/Counter.js)

import classes from './Counter.module.css';
import {useSelector} from 'react-redux';


const Counter = () => {
  const counter = useSelector(state => state.counter);
  const toggleCounterHandler = () => {};

  return (
    <main className={classes.counter}>
      <h1>Redux Counter</h1>
      <div className={classes.value}>{counter}</div>
      <button onClick={toggleCounterHandler}>Toggle Counter</button>
    </main>
  );
};

export default Counter;

here is my package.json:这是我的 package.json:

{
  "name": "redux-basics",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.9",
    "@testing-library/react": "^11.2.5",
    "@testing-library/user-event": "^12.6.3",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-redux": "^7.2.4",
    "react-scripts": "4.0.2",
    "redux": "^4.1.0",
    "web-vitals": "^1.1.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

Error :错误

TypeError: state is undefined
Counter/counter<
C:/Users/nikku/Desktop/redux-project/src/components/Counter.js:6

  3 | 
  4 | 
  5 | const Counter = () => {
> 6 |   const counter = useSelector((state) => state.counter);
  7 |   const toggleCounterHandler = () => {};
  8 | 
  9 |   return (

useSelector should get state as argument by default right? useSelector 应该默认获取状态作为参数,对吗? but here it is saying it is undefined.但这里说它是未定义的。 Ca you please share you thoughts?你能分享一下你的想法吗? Did I do something wrong ?我做错什么了吗 ?

为了初始化默认状态,您需要使用=赋值运算符

const counterReducer = (state = {counter:0},action) => {

Default state is initialized like this.默认状态是这样初始化的。 This will fix your issue.这将解决您的问题。

function appReducer(state = MyInitialState, action) {
 switch (action.type) {
   case "my_custom_type":
      return {
      ...state
    };

   default:
     return state;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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