一、子组件给父组件传参

1-1子组件的属性接收父组件传递过来的方法

  1. //父组件
  2. import React from 'react';
  3. import Title from './components/Title'
  4. class App extends React.Component {
  5. constructor(props){
  6. super(props);
  7. this.state = {
  8. msg:"hello world"
  9. }
  10. }
  11. render() {
  12. return (
  13. <div>
  14. <Title
  15. // 1.子组件的属性接收父组件传递过来的方法
  16. deleteItem = {this.handleDelete.bind(this)}
  17. msg={this.state.msg}></Title>
  18. </div>
  19. )
  20. }
  21. handleDelete(id){
  22. console.log(id)
  23. }
  24. }
  25. export default App;

1-2在子组件调用方法,向组件传参

  1. //子组件
  2. import React from 'react';
  3. class Title extends React.Component {
  4. constructor(props){
  5. super(props)
  6. }
  7. render (){
  8. return (
  9. <h1 onClick={this.handleClick.bind(this,"11313")}>{this.props.msg}</h1>
  10. )
  11. }
  12. handleClick=(id)=>{
  13. // 2.在子组件调用方法,向组件传参
  14. this.props.deleteItem(id)
  15. }
  16. }
  17. export default Title;