PropTypes 与 DefaultProps
import React ,{ Component } from 'react';
import PropTypes from 'prop-types';
class TodoItem extends Component{
constructor(props){
super(props);
this.handleclick=this.handleclick.bind(this);
}
render(){
const { item,test }=this.props;
return (
<div>
<li
onClick={this.handleclick}
// /*dangerouslySetInnerHTML={{__html:item,test}}*/
>{item}-{test}</li>
</div>
)
}
handleclick(){
const { deleteItem,index }=this.props;
deleteItem(index);
}
}
TodoItem.propTypes={ // 要求父组件传递给子组件相关的数据参数类型限制
test:PropTypes.string.isRequired,
item:PropTypes.arrayOf(PropTypes.number,PropTypes.string), //arrayOf指的传递参数要么是数字,要么是字符串
deleteItem:PropTypes.func,
index:PropTypes.number
}
TodoItem.defaultProps={ // 默认传递参数值
test:'hello world'
}
export default TodoItem;