美文网首页
react 父子相互传值方法

react 父子相互传值方法

作者: 糖醋里脊120625 | 来源:发表于2024-01-31 11:33 被阅读0次
1. 父子相互传值函数方法
1.父组件

import React, { useState,useEffect } from 'react'
import { Link, withRouter } from 'react-router-dom'
import Child from './child4';
import { Result, Button } from 'antd'
const NoEorr = () => {
  const state = {
    msg:'我是父组件的信息' //1.父组件准备数据
  }
  const [myname, setMyname] = useState("冠军侯")
  function getChidMsg(childVal){
    setMyname(childVal)
    console.log(childVal)
  }
return ( 
<>
  <Child pMsg={state.msg}  getMsc={getChidMsg}></Child>
  <div>
  <Button type="primary">{myname}</Button>
  </div>
  
   </>
   )
   
  }
export default NoEorr



2.子组件

import React from 'react'
import { Link } from 'react-router-dom'
import { Result, Button } from 'antd'
const Sonny = (prop) => {
  console.log(prop)
  const {pMsg} = prop
  function handClick(){
     console.log(prop)
     prop.getMsc("我从子组件来")
  }
 return (
      <>
        <Button type="primary" onClick={handClick}>这个是子组件</Button>
        <Button type="primary">{pMsg}</Button>
      </>
 )
}
      

export default Sonny

2,还有一种父子相互传值的

1.父组件
import React, { Component } from 'react'
import Son from './child4'
export default class Father extends Component {
  constructor() {
    super()
    this.state = {
      message: 'hello',
      sonVal: null
    }
  }
  render() {
    return (
      <div className="father-ri" id="father">
        <h1>Father组件</h1>

        <button onClick={() => { this.setState({ message: 'world' }) }} > 修改 </button>
        <p>选择了那种水果:{this.state.sonVal}</p>
        <Son data={this.state.message} handle={this.testAction.bind(this)} />

      </div>
    )
  }
  // 接收子组件数据
  testAction(value) {
    console.log(this)
    console.log(value)
    this.setState({ sonVal: value })
  }
}


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

export default class Son extends Component {
  constructor() {
    super()

    this.state = {
      select: '苹果'
    }
  }

  render() {
    let arr = ['苹果', '香蕉', '西瓜']

    return (
      <div className="child-ri" id="son">
        <h1>Son组件</h1>
        <p>接收到的值为:{this.props.data}</p>
        {arr.map((item, index) => {
          return (
            <p key={index}>
              {item}:
              <input
                type="radio"
                value={item}
                checked={this.state.select == item}
                onChange={this.inputChange.bind(this)}
              />
            </p>
          )
        })}
      </div>
    )
  }

  inputChange(ev) {
    let value = ev.target.value
    this.setState({ select: value })
    // 调用父组件的方法,将值传递给父组件
    this.props.handle(value)
  }
}

相关文章

  • 3.组件传值 - service传值

    angular 组件service传值 父子组件相互传值 子组件如果想返回去传值给父组件,父子组件相互传值需要使用...

  • react父子组件相互传值

    1、父组件------>子组件通过父组件设置(state) --------->子组件 (this.props....

  • react-父子组件间通信

    React-父子组件间通信 父组件传 值 给子组件, 传 方法 给子组件 子组件接收 值 ,触发父组件的 方法

  • react数据

    react数据是存在state里面的,在constructor方法里面 父子组件传值放在super(props)这...

  • React父子组件间的传值的方法

    在单页面里面,父子组件传值是比较常见的,这篇文章主要介绍了React父子组件间的传值的方法,小编觉得挺不错的,现在...

  • react Consumer Provider

    跨几个组件传递值或者方法的时候, 如果依赖父子组件传值, 那势必会很麻烦. 好在react提供了Provider ...

  • react父子组件相互传参

    父组件传子: props 子传父:使用回调函数

  • Vue父子组件相互传值

    1. 父组件向子组件传值 子组件在props中创建一个属性,用以接收父组件传过来的值 父组件中注册子组件 在...

  • vue父子组件相互传值

    一、父组件向子组件传值 1、父组件: 2、子组件: 二、子组件向父组件传值 1、父组件 2、子组件: 详情代码地址...

  • react父向子组件通信的形式

    react中是以组件式进行开发,不可避免会有组件层级相互传值的情况。目前将组件通信的方法进行汇总,主要有以下几种。...

网友评论

      本文标题:react 父子相互传值方法

      本文链接:https://www.haomeiwen.com/subject/poaoodtx.html