【问题标题】:redux form not able to store the input value and always return undefinedredux 表单无法存储输入值并始终返回 undefined
【发布时间】:2018-08-23 05:38:25
【问题描述】:

我正在尝试在我的反应登录页面中使用简单的 redux 表单,但是当我在我的电子邮件和密码字段中尝试控制台输出值时,它们总是返回未定义。

这是我的 signin.js

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

class Signin extends Component {
  handleFormSubmit({ email, password }){
    console.log(email,password);
    console.log(this.props.email);
  }

  render(){
    const {  fields: { email, password }, handleSubmit} = this.props;

    return(
        <form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))} >
            <fieldset className="form-group" >
             <label>Email:</label>
             <input {...email} className="form-control" component="input" />
             {console.log(email)}
            </fieldset>
            <fieldset className="form-group" >
             <label>password:</label>
             <input {...password} className="form-control"  />
            </fieldset>
            <button action="submit" className="btn btn-primary"> sign in</button>
        </form>
       );
    }
}

export default reduxForm({
    form: 'signin',
    fields: ['email','password']
})(Signin);

这是我在 reducer 中的 index.js

import { combineReducers } from 'redux';
import { reducer as form } from 'redux-form';

const rootReducer = combineReducers({
  form
});

export default rootReducer;

这是我的 package.json

{
  "name": "redux-simple-starter",
  "version": "1.0.0",
  "description": "Simple starter package for Redux with React and Babel support",
  "main": "index.js",
  "repository": "git@github.com:StephenGrider/ReduxSimpleStarter.git",
  "scripts": {
    "start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js",
    "test": "mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test",
    "test:watch": "npm run test -- --watch"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.2.1",
    "babel-loader": "^6.2.0",
    "babel-preset-es2015": "^6.1.18",
    "babel-preset-react": "^6.1.18",
    "chai": "^3.5.0",
    "chai-jquery": "^2.0.0",
    "jquery": "^2.2.1",
    "jsdom": "^8.1.0",
    "mocha": "^2.4.5",
    "react-addons-test-utils": "^0.14.7",
    "webpack": "^1.12.9",
    "webpack-dev-server": "^1.14.0"
  },
  "dependencies": {
    "babel-preset-stage-1": "^6.1.18",
    "lodash": "^3.10.1",
    "react": "^0.14.3",
    "react-dom": "^0.14.3",
    "react-redux": "4.3.0",
    "react-router": "^2.0.1",
    "redux": "^3.0.4",
    "redux-form": "^7.3.0"
  }
}

我已经完成了所有的搜索,但仍然无法找到这个奇怪问题的解决方案。

我的输入值总是返回 undefined 。

【问题讨论】:

  • 问题是您显然没有阅读redux-form 的文档,或者至少没有看过那里最基本的示例。您需要使用 redux-forms &lt;Field&gt; 组件而不是普通的 &lt;input&gt; 元素。我想知道“完成所有搜索”对您意味着什么,但从我的角度来看,这至少意味着您研究了要使用的框架的文档。
  • 谢谢...我弄错了..并纠正了它

标签: reactjs redux react-redux redux-form


【解决方案1】:

我还没有使用 FieldSet,但是你必须为 redux-form 定义每个字段的名称。我使用了具有 name 属性的 Field 组件。使用 FieldSet 在我看来,您必须使用模型属性来定义 redux-form 的标识符。

【讨论】:

  • 您的回复先生
猜你喜欢
  • 1970-01-01
  • 2020-06-12
  • 2021-12-28
  • 1970-01-01
  • 2019-10-21
  • 1970-01-01
  • 2018-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多