简体   繁体   English

将函数值从父级传递到子级

[英]Passing function value from parent to child

I'm building a check builder, I have multiple Parent components (forms) that display their inputs on a Child component (check layout). 我正在构建一个检查生成器,我有多个父组件(窗体),它们在子组件(检查布局)上显示其输入。

This is my parent component: 这是我的父组件:

import React from 'react';
import './../App.css';
import Check from './Check';

class BusinessAddress extends React.Component {
  constructor(props) {
    super(props);
    this.state = {text: {
      name: 'BusinessAddress',
      a: '',
      b: '',
      c: '',
      d:  '',
      e: '',
      f: '',
      g: '',
      h: ''
    }};

  }

  handleComponentName = name => {
    //console.log('handleComName', name)
    return this.state.name === name;
  }

  handleChange(property, event) {
    console.log(event.target.value);
    const text = {...this.state.text};
    text[property] = event.target.value;
    this.setState({text: text});
  }

  handleSubmit(event) {
    console.log(this.state.text.e);
  }

  render() {

    return (
      <div>
      <div className="form-box">
        <h3>Business Address</h3>
        <label>Business Name</label>
        <input type="text" placeholder="Business Name" value={this.state.text.a} onChange={this.handleChange.bind(this, 'a')} maxLength="30" />
        <label>Name Line 2</label>
        <input type="text" placeholder="Business Name Line 2" value={this.state.text.b} onChange={this.handleChange.bind(this, 'b')} maxLength="90" />
        <label>Street Address</label>
        <input type="text" placeholder="Street Address" value={this.state.text.c} onChange={this.handleChange.bind(this, 'c')} maxLength="30" />
        <label>Address Line 2</label>
        <input type="text" placeholder="Street Address Line 2" value={this.state.text.d} onChange={this.handleChange.bind(this, 'd')} maxLength="30" />
        <label>City</label>
        <input type="text" className="short" placeholder="City" value={this.state.text.e} onChange={this.handleChange.bind(this, 'e')} maxLength="30" />
        <label>State</label>
        <input type="text" className="short" placeholder="State" value={this.state.text.f} onChange={this.handleChange.bind(this, 'f')} maxLength="30" />
        <label>Postal</label>
        <input type="text" className="short" placeholder="Postal" value={this.state.text.g} onChange={this.handleChange.bind(this, 'g')} maxLength="30" />
        <label>Phone (optional)</label>
        <input type="text" className="short" placeholder="Phone" value={this.state.text.h} onChange={this.handleChange.bind(this, 'h')} maxLength="30" />
        </div>

      <Check data={this.state.text} handleParent={this.handleComponentName}/>

      </div>
    )
  }

}

export default BusinessAddress;

This is my child component: 这是我的孩子部分:

import React from 'react';
import './../App.css';

export class Check extends React.Component {
  constructor(props) {
    super(props);
    this.state = {}
  }

  render() {

    const data = Object.values(this.props.data);

    if(this.props.handleParent('BusinessAddress')) {

    }


    return (
      <div>

        { data.map((item, index) => <li key={index}>{item}</li>) }

        <div id="Address"></div>
        <div id="Bank-Info"></div>

      </div>
    )
  }
}



export default Check;

I'm trying to map the data to a specific div based on which parent the data is coming from. 我正在尝试根据数据来自哪个父级将数据映射到特定的div。 Right now all that I'm getting from the child is the returned function, but no return value? 现在我从孩子那里得到的只是返回的函数,但是没有返回值?

Thanks in advance! 提前致谢!

<input type="text" placeholder="Business Name" value={this.state.text.a} onChange={this.handleChange.bind(this, 'a')} maxLength="30" />

Change this to this 改成这个

<input type="text" placeholder="Business Name" value={this.state.text.a} onChange={(e) => this.handleChange(e, 'a')} maxLength="30" />

also change this 也改变这个

handleComponentName = name => {
//console.log('handleComName', name)
return this.state.name === name; }

to

this.state.text.name === name;

here is the demo 这是演示

尝试像这样绑定onchange

onChange={(event) => this.handleChange('h', event)}

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

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