【问题标题】:React component, why are half your props undefined on form submit?React 组件,为什么表单提交时一半的道具未定义?
【发布时间】:2017-01-08 05:55:05
【问题描述】:

React 新手,希望这不是非常明显的事情。一段时间以来,我一直在用头撞墙。

根据 Chrome 中的 React Dev Tools,所有表单的 props 都已定义以及我期望它们是什么。即使提交了表单。太好了。

不幸的是,在 handleSubmit() 中,只有 2 个是可见的:idnamePersonvalue 都是 undefined,尽管在​​开发工具中显示了正确的值。

为什么世界上有一半(而且只有一半)的道具显示为未定义

  import React, {Component} from 'react';
  import {
  TextField,
  IconButton,
  FlatButton,
  DatePicker,
  Snackbar,
  RaisedButton,
  LinearProgress,
  Dialog,
  DropDownMenu,
  MenuItem,
  AppBar
} from 'material-ui';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import EditAttachFile from 'material-ui/svg-icons/editor/attach-file.js';
import NotificationSync from 'material-ui/svg-icons/notification/sync.js';
import EventNote from 'material-ui/svg-icons/notification/event-note.js';

class NameFirstForm extends Component {
  constructor(props) {
    super(props);
    this.state = {value: props.person.getAttr('nameFirst')};


    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  componentWillReceiveProps(nextProps) {
    this.setState({value: nextProps.person.getAttr('nameFirst')});
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  handleSubmit(event) {
    console.log("value", event.target.value);
    event.preventDefault();
    this.props.save.call(this.props.ctx, event);
  }

  render() {
    return (
      <div>
      <form 
        onSubmit={this.handleSubmit}
        name="nameFirst"
        id={this.props.person.getAttr('id')}
        person={this.props.person}
        value={this.state.value} 
      >
        <MuiThemeProvider>
          <TextField 
            name="nameFirst"
            id={this.props.person.getAttr('id')}
            person={this.props.person}
            value={this.state.value} 
            underlineStyle={ {color: 'rgb(0,188,212)' } }  
            floatingLabelFixed={true} 
            floatingLabelText="First Name" 
            onChange={this.handleChange}
          />
        </MuiThemeProvider>
      </form>
        </div>
    );
  }
}

export default NameFirstForm;

【问题讨论】:

    标签: javascript forms reactjs ecmascript-6 undefined


    【解决方案1】:

    您的问题是从表单中读取值。我看到的是,您可以从您的状态和属性中返回具有正确值的对象,如下所示:

    handleSubmit(event) {
        event.preventDefault();
        this.props.save.call(this.props.ctx, {
          person: this.props.person,
          id: this.props.person.getAttr('id'), //You could skip this if you already have the person object
          newValue: this.state.value
        });
    }
    

    【讨论】:

    • 谢谢 Damian,你知道为什么只有一半的道具出现在 handleSubmit() 中吗?我正在重新阅读 React 文档,试图找出我哪里出错了......
    • 如果你的意思是你在 handleSubmit 中看到的只有 name 和 id 的一半属性,那么你对 React 的工作原理有误解。你在handleSubmit 中得到的是一个Form HTML 元素,id 和name 是Form 元素的普通属性。 Person 不是 Form 的普通属性,这就是为什么您在 handleSubmit 中看不到它的原因。无论如何,处理来自表单的值的正确方法应该是通过您的状态变量、属性或直接来自使用 ref 的输入。检查此示例它将帮助您了解如何工作plnkr.co/edit/GPGTJYE2Rlbcyzj5HYTo?p=preview
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 2021-07-20
    • 1970-01-01
    • 2018-11-04
    • 2018-11-12
    • 1970-01-01
    • 2022-08-04
    相关资源
    最近更新 更多