简体   繁体   English

尝试运行时出现“TypeError:无法获取未定义或 null 参考的属性‘年龄’”

[英]Getting “TypeError: Unable to get property 'age' of undefined or null reference” when I try to run

My app is simply supposed to increase the age (initialized to 20) or decrease the age if ADD button or SUBTRACT button is clicked, respectively.如果分别单击 ADD 按钮或 SUBTRACT 按钮,我的应用程序应该只是增加年龄(初始化为 20)或减少年龄。

I am not understanding why I keeping getting this error.我不明白为什么我不断收到此错误。 I check in reducer.js to make sure to initialize the state with the 'age' property accordingly, so I am completely lost as to what might be causing this.我签入 reducer.js 以确保相应地使用“年龄”属性初始化 state,所以我完全不知道可能导致这种情况的原因。 I double checked that I had all the right dependencies, as well as made sure all the syntax is correct.我仔细检查了所有正确的依赖项,并确保所有语法都正确。

One other thing I think might be causing it is that I am not using the spread operator in my reducer.js for the line of code const newState = {state} .我认为可能导致它的另一件事是我没有在 reducer.js 中使用扩展运算符作为代码行const newState = {state} For some reason, vscode is not letting me use the spread operator.出于某种原因,vscode 不允许我使用扩展运算符。 Everytime I do and run my app, nothing appears on screen.每次我执行并运行我的应用程序时,屏幕上都不会出现任何内容。 Is there a dependency that needs to be installed to use the spread operator?是否需要安装依赖项才能使用扩展运算符?

The following is my code:以下是我的代码:

// index.js // index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';

import reducer from './reducer';
import { Provider } from 'react-redux';
import { createStore } from 'redux';

const store = createStore(reducer);

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

// App.js // 应用程序.js

import React from 'react';
import './App.css';
import { connect } from 'react-redux';

class App extends React.Component {
  render() {
    return (
      <div className="App">
        <div>Age: <span>{this.props.age}</span></div>
        <button onClick={this.props.onAgeUp}>Add</button>
        <button onClick={this.props.onAgeDown}>Sub</button>
      </div>
    );
  }
}

const mapStateToProps = (state) => {
  return { age: state.age };
};

const mapDispatchToProps = (dispatch) => {
  return {
    onAgeUp: () => dispatch({ type: 'ADD' }),
    onAgeDown: () => dispatch({ type: 'SUBTRACT' })

  };
};

export default connect(mapStateToProps, mapDispatchToProps)(App);

// reducer.js // 减速器.js

import { ADD, SUBTRACT } from './actionTypes';

const initialState = {
  age: 20
};

const reducer = (state=initialState, action) => {
    const newState = {state};

    switch(action.type) {
        case ADD:
            newState.age+=1;
            break;
        case SUBTRACT:
            newState.age-=1;
            break;
        default:
            return state;
    }

};

export default reducer;

Quick fix, You can use Object.assign instead of spread operator:快速修复,您可以使用Object.assign代替传播运算符:

const newState = Object.assign({}, state);

For using spread operator you must check couple of things:要使用扩展运算符,您必须检查几件事:

first make sure @babel/plugin-proposal-object-rest-spread dependency is in package.json , then add it to plugins of babel config file:首先确保@babel/plugin-proposal-object-rest-spread依赖项在package.json中,然后将其添加到 babel 配置文件的插件中:

{
  "plugins": ["@babel/plugin-proposal-object-rest-spread"]
}

for more info visit babel rest spread欲了解更多信息,请访问babel rest 传播

I see three problems here:我在这里看到三个问题:

  • You're declaring newState as {state}.您将 newState 声明为 {state}。 This creates object within object.这将在 object 中创建 object。
  • Since you're declaring newState with const, it can not be changed.由于您使用 const 声明 newState,因此无法更改。 But you're trying to change newState in both of your case.但是你在这两种情况下都试图改变 newState 。
  • You're not returning anything to the state.您不会向 state 返还任何东西。 Main idea of the redux is to update the state by returning the new state. redux 的主要思想是通过返回新的 state 来更新 state。

newState looks like this the way you define it: newState 看起来像你定义它的方式:

{
  state: {
    age: 20
  }
}

That's why age is undefined, it's not an immediate child property.这就是年龄未定义的原因,它不是直接子属性。

Eg:例如:

const letters = { alpha: 'a' }

const spreadLetters = { letters }

spreadLetters will be a new object, with one property, "letters". spreadLetters 将是一个新的 object,具有一个属性“字母”。 The value of this property will be an exact copy of the original letters variable.此属性的值将是原始字母变量的精确副本。

{
  letters: {
    alpha: a
  }
}

暂无
暂无

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

相关问题 TypeError:无法获取未定义或空引用的属性“0” - TypeError: Unable to get property '0' of undefined or null reference 我一直在“无法获取未定义或空引用的属性”长度” - I keep getting 'unable to get property 'length' of undefined or null reference' TypeError:无法获取未定义或空引用的属性“项目” - TypeError: Unable to get property 'project' of undefined or null reference 反应:TypeError:无法获取未定义或空引用的属性“处理程序” - React: TypeError: Unable to get property 'handler' of undefined or null reference TypeError:无法在IE 10上获取未定义或空引用的属性&#39;ngMetadataName&#39; - TypeError: Unable to get property 'ngMetadataName' of undefined or null reference on IE 10 RequireJS:定义模块-TypeError:无法获取未定义或空引用的属性 - RequireJS: Define a module - TypeError: Unable to get property of undefined or null reference 无法获取未定义或空引用的属性“未定义” - Getting Unable to get property 'undefined' of undefined or null reference 错误类型错误:无法获取未定义或 null 参考的属性“0” - 当在控制台中的 HTML_Element 上单击()时 - ERROR TypeError: Unable to get property '0' of undefined or null reference - When click() on a HTML_Element in the Console 获取此错误 - 无法获取未定义或空引用的属性“mData” - Getting this error - Unable to get property 'mData' of undefined or null reference 混淆获取此错误提示器无法获取未定义或空引用的属性“检查” - Confusion Getting this Errorr Unable to get property 'checked' of undefined or null reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM