React - Can A Child Component Send Value Back To Parent Form

前端 未结 3 1605
悲&欢浪女
悲&欢浪女 2020-12-23 11:52

The InputField & Button are custom components that go into a form to create a form. My issue is how do I send the data back up to form so that

3条回答
  •  太阳男子
    2020-12-23 12:30

    Parent.js

    import React, { Component } from 'react';
    import Child from './child'
    class Parent extends Component {
      state = {
        value: ''
      }
      onChangeValueHandler = (val) => {
        this.setState({ value: val.target.value })
      }
      render() {
        const { value } = this.state;
        return (
          

    the value is : {value}

    ); } } export default Parent;

    Child.js

      import React, { Component } from 'react';
      class Child extends Component {
      render() {
      const { value , onChangeValue } = this.props;
      return (
        
    ); } } export default Child;

    you can see a live example on : https://codesandbox.io/s/two-way-binding-qq1o1?from-embed

提交回复
热议问题