this.p...">
美文网首页
react-组件之间的传值和参数校验

react-组件之间的传值和参数校验

作者: 四月的谎言v5 | 来源:发表于2019-07-13 11:07 被阅读0次

有如下2个组件

import React, { Component } from 'react'

export default class MyComponent extends Component {
  render() {
    return (
      <div>
        <span>hello,world!</span>
      </div>
    )
  }
}

import React, { Component } from 'react'
import MyConponent from '../components/MyComponent'
export default class MyPage extends Component {
  render() {
    return (
      <div>
        <MyConponent />
      </div>
    )
  }
}

\color{green}{修改为}

import React, { Component } from 'react'
import MyConponent from '../components/MyComponent'
export default class MyPage extends Component {
  render() {
    return (
      <div>
        <MyConponent myPageValue="我是MyPage组件的值" />
      </div>
    )
  }
}
import React, { Component } from 'react'

export default class MyComponent extends Component {
  render() {
    return (
      <div>
        <span>hello,world!</span>
        <p>{this.props.myPageValue}</p>
      </div>
    )
  }
}

\color{green}{效果图}

1.png

<MyConponent myPageValue="我是MyPage组件的值" /> \color{green}{可以看出使用组件时可以直接在组件上写属性 然后通过}this.props\color{green}{对象得到所有传递过来的属性以及方法}

\color{green}{下面来看看子组件通过调用父组件方法传值}

import React, { Component } from 'react'

export default class MyComponent extends Component {
  render() {
    return (
      <div>
        <span>hello,world!</span>
        <p>{this.props.myPageValue}</p>
        <button onClick={() => this.props.update('我修改了父组件value')}>
          修改
        </button>
      </div>
    )
  }
}

注意一下 this.updataMyPageValue.bind(this) \color{green}{bind(this)} 绑定父组件的this指针不然子组件调用之后this就不是父组件了就无法修改父组件的satate了

import React, { Component } from 'react'
import MyConponent from '../components/MyComponent'
export default class MyPage extends Component {
  constructor(props) {
    super(props)
    this.state = {
      value: '未改变'
    }
  }
  render() {
    return (
      <div>
        <MyConponent
          update={this.updataMyPageValue.bind(this)}
          myPageValue="我是MyPage组件的值"
        />
        <p>{this.state.value}</p>
      </div>
    )
  }
  updataMyPageValue(value) {
    this.setState({
      value
    })
  }
}

\color{green}{按钮点击前}

1.png

\color{green}{按钮点击后}

1.png

\color{green}{接下来是参数校验}

npm install prop-types 先安装参数校验包

import React, { Component } from 'react'
import PropTypes from 'prop-types'

export default class MyComponent extends Component {
  static propTypes = {
    myPageValue: PropTypes.string.isRequired,
    update: PropTypes.func.isRequired
  }
  render() {
    return (
      <div>
        <span>hello,world!</span>
        <p>{this.props.myPageValue}</p>
        <button onClick={() => this.props.update('我修改了父组件value')}>
          修改
        </button>
      </div>
    )
  }
}

\color{green}{没看错就是这样是不是很简单}

 static propTypes = {
    myPageValue: PropTypes.string.isRequired,
    update: PropTypes.func.isRequired
  }

myPageValue:PropTypes.string.isRequired \color{green}{myPageValue}必须是字符串并且是必须的
update: PropTypes.func.isRequired \color{green}{update}必须是方法并且是必须的

更详细的规则请点击查看

我把MyPage改成如下

 <MyConponent
          update={this.updataMyPageValue.bind(this)}
          myPageValue={1/**传入一个数字 */} 
        />
1.png
虽然能执行但是控制台会报一个警告

相关文章

  • react-组件之间的传值和参数校验

    有如下2个组件 this.p...

  • 【Vue3 从入门到实战 进阶式掌握完整知识体系】011-探索组

    2、组件间传值及传值校验 父组件给子组件传值 运行结果 父组件给子组件传动态参数 运行结果 子组件校验父组件的传参...

  • react-父子组件间通信

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

  • 组件之间的传值

    组件之间的传值,包括父子组件传值,兄弟组件之间的传值,其中父子组件包括父组件向子组件传值和子组件向父组件传值,现在...

  • 六、组件参数校验与非props特性

    父组件可以向子组件传值,但是相应的,子组件可以对从父组件传过来的值进行约束,这就是组件参数校验。举一个需求,假设父...

  • 2019-03-13

    vue父子组件传值,(父组件向子组件传值用prop ,子组件向父组件传值:子组件调用父组件方法值以参数的方式传递)...

  • 2019-03-13

    vue父子组件传值,(父组件向子组件传值用prop ,子组件向父组件传值:子组件调用父组件方法值以参数的方式传递)...

  • 组件通信

    vue传值可分为父子之间传值、兄弟组件之间传值、跨代组件之间传值 1.父子之间传值:可以使用$emit/props...

  • Vue.js 父子组件传值 . 兄弟组件传值

    概述 vue中组件之间的传值传值情况主要有以下三种 父组件向子组件传值子组件向父组件传值兄弟组件之间相互传值或者是...

  • Vue3.2组件间通信

    组件之间的传值及通信 一、第一种场景 两层组件之间传值和监听值改变 二、第二种场景 多层组件间传值和监听值改变父组...

网友评论

      本文标题:react-组件之间的传值和参数校验

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