反应|将数据从父组件传递到子组件

时间:2018-02-23 17:04:27

标签: reactjs react-props

在React中,我有像

这样的文件
--parent.js
--child.js
--App.js

parent.js 包含文本框和按钮

child.js 包含P标记

App.js 包含父组件和子组件

问题

将“按钮上的文本框”值从按钮单击传递到子项,并在子段落标记中显示该值。

完整代码 stackblitz

3 个答案:

答案 0 :(得分:1)

更新了您的示例以将数据传递给子组件。

https://stackblitz.com/edit/react-trmj9i?file=child.js

添加以下代码以供快速参考

<强> index.js

import React, { Component } from 'react';
import { render } from 'react-dom';
import Parent from './parent';
import Child from './child';
import './style.css';

class App extends Component {
  constructor() {
    super();
    this.state = {
      name: 'React',
      parentTextBoxValue: ''
    };
  }
  handleParentData = (e) => {
this.setState({parentTextBoxValue: e})
  }

  render() {
    return (
      <div>
        <Parent handleData={this.handleParentData} />
        <Child parentTextBoxValue={this.state.parentTextBoxValue}/>
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

<强> parent.js

import React, { Component } from 'react';
import Button from 'react-uikit-button';

class Parent extends Component {

constructor(props){
    super(props);
    this.state={TextBoxValue: ""}
}   

  SubmitValue = (e) => {
     this.props.handleData(this.state.TextBoxValue)
  }

   onChange=(e)=>{
this.setState({TextBoxValue: e.target.value})
   } 
  render() {
    return (
      <div className="">
        <input type="text" name="TextBox"  onChange={this.onChange}
         />
        <Button onClick={this.SubmitValue}>Submit Value</Button>
      </div>
    );
  }
}

export default Parent;

<强> child.js

import React, { Component } from 'react';

class Child extends Component {

    constructor(props){
        super(props);
    }


  render() {
    return (
      <div className="App">
       <p>{this.props.parentTextBoxValue}</p>
      </div>
    );
  }
}

export default Child;

只是为了给我所做的, 将一个函数从App.js传递给父级,可以帮助提升状态。 在父组件的文本框中处理onchange并提交更新的应用程序状态。 最后将这个状态传递给子组件。

答案 1 :(得分:0)

在按钮上单击,您应该能够获取文本框的值,并使用setState函数将其添加到父级的状态。

之后,应调用父母的渲染方法;因为,国家改变了。然后,您可以将状态中保存的值放入子元素的属性中。

<child message={value}>

之后,您可以通过孩子的道具访问该消息。

class child extends Component {
    render(){
        //use this.props.message to access message
    }
}

从那里你可以随心所欲地做任何事情。

答案 2 :(得分:0)

在App组件内

import React, { Component } from 'react';
import Parent from './Parent';

class App extends Component {
  state= {
    data: '',
  }

  addFormValue = (data) => {
    this.setState({
      data,
    });   
  }

  render() {
    return (
      <div>
        <Parent addFormValue={this.addFormValue} data={this.state.data}/>
      </div>
    )
  }
}

export default App;

在“父”组件中

import React, { Component } from 'react';
import Child from './Child';

class Parent extends Component {
  constructor({props}) {
      super(props);
  }

  textBoxRef = React.createRef();

  getValue = (event) => {
    event.preventDefault();

    const textBoxValue = this.textBoxRef.value.value;            
    this.props.addFormValue(textBoxValue);
  };

  render() {
    return (
    <div>
      <form>
        <input type="text" name="name" placeholder="Name" ref={this.textBoxRef}/>
        <button type="submit" onClick={this.getValue}>Submit</button>
      </form>

      <Child data={this.props.data} />
    </div>
    );
  }
}

export default Parent;

在子组件中

import React from 'react'; 
    const Child = ({data}) => (
      <p>{data}</p>
    );  

export default Child

View on CodeSandbox

相关问题