简体   繁体   English

如何在父子组件之间传递数据

[英]how to Pass Data between Parent and Child Components

i am learning to pass data between parent and child components via a tutorial. 我正在学习通过教程在父子组件之间传递数据。 i double checked my code and still couldn't figure out the mistake. 我仔细检查了我的代码,但仍然无法找出错误。

my index.js 我的index.js

import React, {Component} from 'react';
import ReactDom from 'react-dom';
import {Header} from './components/Header';
import {Home} from './components/Home';

class App extends Component {
  constructor(){
    super();
    this.state ={
      HomeLink:"home"
    };
  }
  onGreet(){
    alert("hello");
  }

  onChangeLinkName(newName){
    this.setState({
      HomeLink:newName
    });
  }
  render(){
    return (
      <div className="container">
        <div className="row">
          <div className="col-xs-10 col-xs-offset-1">
            <Header HomeLink={this.state.HomeLink} />
          </div>
        </div>
        <div className="row">
          <div className="col-xs-10 col-xs-offset-1">
            <Home
            name={"max"}
             initialAge={27}
             greet={this.onGreet}>
             changeLink={this.onChangeLinkName.bind(this)}
              <p>This is a paragraph passed from props</p>
            </Home>
          </div>
        </div>
      </div>
    );
  }
}
ReactDom.render(
  <App/>, window.document.getElementById("root")
);

Home.js Home.js

the button change link onClick event is calling changeLink function which pass the newlink to props. 按钮更改链接onClick事件正在调用changeLink函数,该函数将newlink传递给props。

import React from 'react';

export class Home extends React.Component {
  constructor(props){
    super();
    this.state = {
      age:props.initialAge,
      status:0,
      homeLink:"changed link"
    };
    setTimeout(() => {
      this.setState({
        status:1
        });
    },3000);
  }
  onMakeOlder(){
    this.setState({
      age:this.state.age+3
    });
  }
  onChangeLink(){
    this.props.changeLink(this.state.homeLink);
  }

  render(){
    return(
      <div>
        <p>In a new Component</p>
        <p>Your name is {this.props.name} and your age is {this.state.age}</p>
        <p>{this.state.status}</p>
        <hr/>
        <button onClick={()=> this.onMakeOlder()}>make me older</button>
        <hr/>
        <button onClick={this.props.greet}>greet</button>
        <hr/>
        <button onClick={this.onChangeLink.bind(this)}>change link</button>
      </div>
    );
  }
}

Home.propTypes = {
  name:React.PropTypes.string,
  age:React.PropTypes.number,
  user:React.PropTypes.object,
  children: React.PropTypes.element.isRequired,
  greet:React.PropTypes.func,
  changeLink:React.PropTypes.func
};

the error:- 错误:-

Uncaught TypeError: this.props.changeLink is not a function

There is a typo in the code. 代码中有错字。

<Home
  name={"max"}
  initialAge={27}
  greet={this.onGreet}>
  changeLink={this.onChangeLinkName.bind(this)}
    <p>This is a paragraph passed from props</p>            
</Home>

Notice how you are closing the Home component right after setting the property greet with this character : > . 请注意,设置属性以以下字符greet后,如何立即关闭Home组件: > You want to close it after you set changeLink as a prop so move that > down to after this.onChangeLinkName.bind(this)} 您想要在将changeLink设置为prop后关闭它,所以将>向下移动到this.onChangeLinkName.bind(this)}

That will fix your error and you should be able to move forward. 这将解决您的错误,您应该可以继续前进。

Switch the prop you're passing to: 将您要传递的道具切换到:

changeLink={this.onChangeLinkName}

and in the constructor of the parent: 并在父级的构造函数中:

this.onChangeLinkName = this.onChangeLinkName.bind(this);

Lastly, in your button call: 最后,在您的按钮呼叫中:

<button onClick={this.props.changeLink(this.state.homeLink)}>change link</button>

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

相关问题 如果它们不是Angular2中的父子组件,如何在组件之间传递数据? - How to pass data between components, if they are not parent-child components in Angular2? 如何在不使用服务的情况下在子组件与父组件之间传递反应式表单数据 - How to pass reactive form data between child to parent components with out using services vue中如何在父子组件之间传递值? - How to pass values between parent and child components in vue? 如何将对象数据从父组件传递到子组件? - How to pass object data from parent to child components? 如何使用ChangeDetectionStrategy在父子组件之间进行数据更新 - How to use ChangeDetectionStrategy for data update between parent-child components 如何在不渲染子组件的情况下在 React 组件之间传递数据 - How to pass data between React components without rendering the child component 如何在本机与子代之间传递数据? - How to pass data between child and parent in react-native? 如何在Reactjs中将道具从父组件传递到子组件 - How to pass props from parent components to child components in reactjs 将 vue 中的数据从单个父组件传递到子组件 - Pass data in vue from single parent component to child components 组件渲染后如何在reactJS中将数据从父级传递给子级 - How to pass data from parent to child in reactJS after the components have rendered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM