简体   繁体   English

如何从表单组件中获取状态/值?

[英]How to get state / value from form component?

Consider having form component like: 考虑使用表单组件:

export default class Form extends React.Component {
  constructor (props) {
    this.state = { email: '' }
    this.onChange = this.onChange.bind(this)
  }

  onChange(event) {
    this.setState({ email: event.target.value })
  } 

  render () {
    return (
      <div>
        <h2>{this.props.title}</h2>
        <form className={cx('Form')} onSubmit={this.props.onSubmit}>
          <input className={cx('Form-email')} type='email' placeholder='email' value={this.state.email} onChange={this.onChange} />
          <input className={cx('Form-btn')} type='submit' value='sign up' />
        </form>
      </div>
    )
  }
}

I would then use this <Form onSubmit={this.someFunction} /> component elsewhere within my app, lets assume inside HomePage component. 然后我会在我的app中的其他地方使用这个<Form onSubmit={this.someFunction} />组件,让我们假设在HomePage组件中。 Inside that home page I would have this.someFunction that executes when form is summited, how can I pass form value / state to it? 在该主页内部,我将拥有在表单传递时执行的this.someFunction ,如何将表单值/状态传递给它?

Create a callback in your component that will call the function sent to Form with the state as parameter. 在组件中创建一个回调函数,该回调函数将调用发送到Form的函数,并将state作为参数。

export default class Form extends React.Component {
    constructor (props) {
        this.state = { email: '' }
        this.onChange = this.onChange.bind(this)
        this.onSubmit = this.onSubmit.bind(this)
    }

    onChange(event) {
        this.setState({ email: event.target.value })
    } 

    onSubmit() {
        this.props.onSubmit(this.state);
    } 

    render () {
        return (
            <div>
                <h2>{this.props.title}</h2>
                <form className={cx('Form')} onSubmit={this.onSubmit}>
                    <input className={cx('Form-email')} type='email' placeholder='email' value={this.state.email} onChange={this.onChange} />
                    <input className={cx('Form-btn')} type='submit' value='sign up' />
                </form>
            </div>
        )
    }
}

What you're (essentially) looking to do is pass some data up the component chain (to a parent component). 您(本质上)要做的是将一些数据传递到组件链(到父组件)。 You could implement this with vanilla React, but I'm not going to advise you to do this. 可以用vanilla React实现这个,但我不打算建议你这样做。

If you try implementing some kind of state management yourself, unless your app is incredibly simple or you are an incredibly disciplined one-man-team, it's likely to get messy and unpredictable fast. 如果您尝试自己实施某种状态管理,除非您的应用程序非常简单,或者您是一个非常自律的单人团队,否则很快就会变得混乱和难以预测。

I advocate one way data flow . 我主张单向数据流 Data should flow one way through your app - down. 数据应该通过您的应用程序向下流动 - 向下。 I recommend you look at implementing a solution with Flux or Redux (Redux is my preference). 我建议您考虑使用FluxRedux实现解决方案(Redux是我的首选)。 These are both state containers that will propagate state throughout your app and enforce a set of conventions which you help you maintain structure to your data flow as your app grows. 这些都是状态容器 ,它们将在整个应用程序中传播状态,并强制执行一组约定,以帮助您在应用程序增长时维护数据流的结构。

I admit, you're adding to the learning curve by implementing a solution with these containers, but remember that React is only the view layer and it can't help you much with problems surrounding state management. 我承认,通过使用这些容器实现解决方案,您将增加学习曲线,但请记住,React 只是视图层,并且它无法帮助您解决有关状态管理的问题。

You could do this: 你可以这样做:

export default class Form extends React.Component {
  constructor (props) {
    this.state = { email: '' }
    this.onChange = this.onChange.bind(this)
    this.onSubmit = this.onSubmit.bind(this)
  }

  onChange(event) {
    this.setState({ email: event.target.value })
  }

  // Wrap around this.props.onSubmit and add data to it.
  onSubmit() {
    this.props.onSubmit(this.state);
  }

  render () {
    return (
      <div>
        <h2>{this.props.title}</h2>
        <form className={cx('Form')} onSubmit={this.onSubmit}>
          <input className={cx('Form-email')} type='email' placeholder='email' value={this.state.email} onChange={this.onChange} />
          <input className={cx('Form-btn')} type='submit' value='sign up' />
        </form>
      </div>
    )
  }
}

Very similar to how you bound and use your onChange. 非常类似于绑定和使用onChange的方式。

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

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