• props 传递数据
    • props 传递函数
    • props 类型检查

    vue是通过 this.$emit(‘name’)

    1. import PropTypes from 'prop-type'
    2. class List extends React.Component {
    3. constructor(props) {
    4. super(props)
    5. }
    6. render() {
    7. const {list} = this.props;
    8. return
    9. <ul>
    10. {
    11. list.map((item) => <p key={item.id}>{item.title}</p>)
    12. }
    13. </ul>
    14. }
    15. }
    16. // 类型检查
    17. List.propTypes = {
    18. list: PropTypes.arrayOf(PropTypes.object).isRequired
    19. }
    20. class Input extends React.Component {
    21. constructor(props) {
    22. super(props)
    23. this.state = {
    24. title: ''
    25. }
    26. }
    27. render() {
    28. return <div>
    29. <input value={this.state.title} onChange={this.onTitleChange} />
    30. <button onClick={this.onSubmit}>submit</button>
    31. </div>
    32. }
    33. onSubmit = () => {
    34. this.props.onSubmitTitle(this.state.title);
    35. }
    36. }
    37. class TodoList extends React.Component {
    38. // 状态提升,本身是List组件使用的数据,被提升到上一层的组件中去
    39. render() {
    40. return <div>
    41. <Input submitTitle={this.onSubmitTitle}/>
    42. <List list={this.state.list} />
    43. </div>
    44. }
    45. }