直接上代码

//父组件
import React, { Component } from 'react'
import Home from '../home/home'
export default class login extends Component {
  constructor(props) {
    super(props)
    this.state = {
      name: '我是父组件',
      msg: '父组件传值给子组件',
      childrenMsg: ''
    }
  }
  handleDelete(item) {
    console.log(this);
    console.log(item);
    this.setState({
      childrenMsg: item
    })
  }

  render() {
    return (
      <div>
        <h2>{this.state.name}</h2>
        <h3>子组件传来的值为:{this.state.childrenMsg}</h3>
        <h3>我要引入子组件了:</h3>
        <hr />
        <Home title={this.state.msg} parentDelete={this.handleDelete.bind(this)} />
      </div>
    )
  }
}

//子组件
import React, { Component } from 'react'

export default class home extends Component {
  constructor(props) {
    super(props)
    this.state = {
      name: '我是子组件',
      msg: '子组件传值给父组件'
    }
  }
  componentDidMount() {
    this.props.parentDelete(this.state.msg);
  }
  render() {
    return (
      <div>
        <h2>{this.state.name}</h2>
        <p>{this.props.title}</p>
      </div>
    )
  }
}

Lifelong learning lifelong benefit