【问题标题】:TypeError: Cannot read property 'any' of undefined ReactTypeError:无法读取未定义 React 的属性“任何”
【发布时间】:2018-10-23 16:21:02
【问题描述】:

我是 React 新手,我正在尝试按照 Udemy 教程制作小应用程序。该应用程序包含一个表格。当我将redux-form 库导入我的项目时,出现以下控制台错误。

package.json

"dependencies": {
    "axios": "^0.18.0",
    "babel-preset-stage-1": "^6.1.18",
    "lodash": "^3.10.1",
    "react": "16.3.2",
    "react-countdown-now": "^1.3.0",
    "react-dom": "16.3.2",
    "react-redux": "5.0.7",
    "react-router": "^3.2.0",
    "redux": "4.0.0",
    "redux-form": "^4.1.3",
    "redux-promise": "^0.5.3"
  }

post_new.js 包含表单。

import React, { Component } from 'react';
import { reduxForm } from 'redux-form';

class PostsNew extends Component {
    render() {
        return (
            <form onSubmit={handleSubmit}>
                <h3>Create a new post</h3>
                <div className="form-group">
                    <label>Title</label>
                    <input type="text" className="form-control" />
                </div>
                <div className="form-group">
                    <label>Categories</label>
                    <input type="text" className="form-control"/>
                </div> 
                <div className="form-group">
                    <label>Content</label>
                    <textarea className="form-control"/>
                </div>

                <button type="submit" className="btn btn-primary">Submit</button>
            </form>    
        );
    }
}
export default reduxForm(
    {
        form: 'PostsNewForm',
        fields: ['title','categories','content']
    }
)(PostsNew);

还在减速器中导入redux-form _> index.js

import { combineReducers } from 'redux';
import PostReducer from './reducer_posts';
import { reducer as formReducer } from 'redux-form';

const rootReducer = combineReducers({
  posts: PostReducer,
  form: formReducer
});

export default rootReducer;

【问题讨论】:

  • 尝试更新你的 redux-form。您已经在使用版本 5 n v6。 redux-form.com/6.7.0/docs/migrationguide.md
  • 如果您在导入 redux-form 之前没有任何错误,那么它肯定与您拥有的 redux-form 版本存在一些兼容性问题。尝试更改版本。'
  • 我已将 redux-form 版本更新到 v7。现在它是 "redux-form": "^7.3.0"。但错误仍然存​​在。
  • 听起来你在某些时候正在使用一个对象并试图读取它们的键 any。当您尝试访问像 this.props.data.any 这样的嵌套对象中的道具时,这很常见,其中 this.props.data 在某些时候是 undefined

标签: javascript reactjs react-redux typeerror


【解决方案1】:

使用版本 5.3.6。 肯定能解决问题。

【讨论】:

    【解决方案2】:

    我也有同样的错误。我如何解决它是将redux-form升级到v5.2.6。然后使用 Ctrl +c 在终端中停止服务器,运行 npm i 以引入 redux -form 依赖项。然后重启你的项目。

    【讨论】:

    • 我认为没有redux-form v5.2.6。
    • 你说得对,我的意思是 redux-form: "^5.3.6"。谢谢指正
    【解决方案3】:

    使用包管理器安装更新是不够的。 更新 redux-form 后,您需要重新安装 localhost。 使用:

    • Ctrl + 'C'
    • npm i
    • npm 开始

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 2021-09-24
      • 2020-07-10
      • 2019-07-07
      • 2021-03-10
      • 2020-02-10
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多