|
- import React, { Fragment, Component } from 'react';
- import PropTypes from 'prop-types';
- class ToDoItem extends Component
- {
- // 父子组件传值
- // 组件之间的传值靠 属性
- // 第一步 在 父组件【其实不是负的也对啊】 <Myapp mydata={dsss} /> 上绑定 属性
- // 第二步 在 组件内部 使用 this.props.mydata 来接受 传递过来的值 与 方法
- // 里面的this 指向问题
- // 先说一个简写 在 constructor 中 把 函数指定了 this 则 下面不用指定
- // 再说一个 父级this 的绑定问题 ; 因为方法是用 属性传过来的 .直接在父级页面 bind(this) 即可把 子页面接收到的方法 指向调整为this
- constructor(props)
- {
- super(props);
- this.delFunction = this.delFunction.bind(this); // 和在行间写一样
- }
- render(){
- let {delF, content,index} = this.props;
- return (
- <Fragment>
- <li onClick={this.delFunction} >
- {content }
- </li>
-
- </Fragment>
- );
- }
- delFunction()
- {
- let {delF, content,index} = this.props;
- // alert(this.props.index )
- // 花样传值
- delF( index )
- }
- }
- // 这玩意是用于规定 组件传过来的 值的类型的 必须写在 类的 外面
- // string 字符串; func 函数 ;number 数字 ;还有数组-对象之类 还可以 即使字符串又是数字
- // 如果 后面接了 .isRequired 表示是必须传递过来的值 例如 PropTypes.string.isRequired
- ToDoItem.propTypes ={
- content:PropTypes.string,
- delF:PropTypes.func
- }
- // 默认值
- ToDoItem.defaultProps = {
- content:'sdyu'
- }
- export default ToDoItem;
复制代码
|
|