【问题标题】:React | pass data from parent to child component反应 |将数据从父组件传递到子组件
【发布时间】:2023-04-03 18:17:01
【问题描述】:

在 React 中,我有类似的文件

--parent.js
--child.js
--App.js

parent.js 包含文本框和按钮

child.js 包含 P 标签

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

问题

在按钮单击时将文本框值从父级传递给子级,并在子段落标记中显示该值。

完整代码 stackblitz

【问题讨论】:

标签: reactjs react-props


【解决方案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 和提交更新的应用程序状态。 最后将此状态传递给子组件。

【讨论】:

    【解决方案2】:
    import React from "react";
    
    class Parent extends React.Component(){
        constructor(props){
            super(props);
            this.state={
                name:"suraj"
            }
        }
    
        render(){
            return(
                <div className = "parent">
                    <child data={this.state.name}/> 
                </div>
            );
        }
    
    }
    
    export default Parent;
    
    export function Child(props){
        return(
            <div>{props.data}</div>
        );
    }
    

    【讨论】:

      【解决方案3】:

      在父组件内

      import React, { useState } from "react";
      import Child from "./Child";
      
      export default function App() {
        const [value, setValue] = useState("");
      
        const handleInputChange = event => {
          const { value } = event.target;
          setValue(value);
        };
      
        const handleSubmit = event => {
          event.preventDefault();
          console.log(value);
        };
      
        return (
          <div className="App">
            <form onSubmit={handleSubmit}>
              <input
                type="text"
                name="name"
                placeholder="Name"
                onChange={handleInputChange}
              />
              <button type="submit">Submit</button>
            </form>
      
            <Child data={value} />
          </div>
        );
      }
      

      在子组件内

      import React from "react";
      
      export default function Child({ data }) {
        return (
          <div className="App">
            <h1>Hello Child</h1>
            <p>{data}</p>
          </div>
        );
      }
      

      View on CodeSandbox

      【讨论】:

      • 感谢您发布函数组件而不是类,因为函数组件越来越受欢迎。
      【解决方案4】:

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

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

      <child message={value}>
      

      之后,您可以通过 child 中的 props 访问该消息。

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

      从那里你可以对价值做任何你想做的事情。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-04
      • 2019-02-27
      • 2017-04-20
      • 2021-12-24
      • 2020-11-21
      • 2019-10-13
      相关资源
      最近更新 更多