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

2019-07-13  本文已影响0人  四月的谎言v5

有如下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
虽然能执行但是控制台会报一个警告
上一篇 下一篇

猜你喜欢

热点阅读