纵横研究院React技术专题社区

【学习笔记 】React ② 组件拆分以及组件间通信

2019-11-28  本文已影响0人  Moombahton

目录

github地址:https://github.com/cindygogogo/studyReact/tree/master/todolist

组件拆分

接上,将todoList组件进行拆分,结构如下


todolist组件结构.png

父子组件之间通过标签属性的方式进行通信

1.父组件向子组件传值

 <TodoItem
  content={item}
  index={index}  
  deleteItem={this.handleItemDelete.bind(this)} />
<div >{this.props.content}</div>

2.子组件调用父组件的方法,修改父组件的内容
子组件通过this.props.func()就可以调用父组件的方法,父组件传递的函数 this指向要做绑定,借助这个方法对父组件的数据进行修改

Todoitem.js

import React, { Component } from 'react'

class TodoItem extends React.Component{
    constructor (props) {
        super(props)
        this.handleClick = this.handleClick.bind(this)
    }
    render () {
        return <div onClick={this.handleClick}>{this.props.content}</div>
    }
    handleClick () {
       this.props.deleteItem(this.props.index)
    }

}

export default TodoItem

在Todolist.js中引入Todoitem组件,修改<ul></ul>标签包裹的内容

import TodoItem from './TodoItem'

<ul>
    {
          this.state.list.map((item, index) => {
                return (
                     <div>
                         <TodoItem
                           content={item}
                           index={index}
                           deleteItem={this.handleItemDelete.bind(this)}
                        />
                   </div>
              )
          })
     }
</ul>

todolist代码优化

1.优化todoitem组件,更符合ES6的写法,例如const { content } = this.props
完整代码:

import React, { Component } from 'react'

class TodoItem extends React.Component{
    constructor (props) {
        super(props)
        this.handleClick = this.handleClick.bind(this)
    }
    render () {
        const { content } = this.props
        return (
                <div onClick={this.handleClick}>
                    {content}
                </div>
            )

    }
    handleClick () {
        const { deleteItem, index } = this.props
        deleteItem(index)
    }

}

export default TodoItem

2.优化todilist组件
减少页面中的JS逻辑getTodoItem ()
在组件初始化的时候,修改this绑定

this.handleInputChange = this.handleInputChange.bind(this)

使用更推荐的方法修改修改数据

   const value = e.target.value
        this.setState( () => ({
            inputValue: value
        }) )

完整代码:

import React, {Fragment, Component} from 'react';
import TodoItem from './TodoItem'
import './style.css'

class Todolist extends React.Component{
    constructor (props) {
        super(props)
        this.state = {
            inputValue: '',
            list: []
        }
        // 组件初始化的时候修改this绑定
        this.handleInputChange = this.handleInputChange.bind(this)
        this.handleBtnClick = this.handleBtnClick.bind(this)
        this.handleItemDelete = this.handleItemDelete.bind(this)
    }
    render() {
        return (
            <Fragment>
                <div>
                    <label htmlFor="insertArea">输入内容</label>
                    <input
                        id="insertArea"
                        className="input"
                        value={this.state.inputValue}
                        onChange={this.handleInputChange}/>
                    <button onClick={this.handleBtnClick}>提交</button>
                </div>
                <ul>
                  { this.getTodoItem() }
                </ul>
            </Fragment>
        );
    }
    getTodoItem () {
       return this.state.list.map((item, index) => {
            return (
                <TodoItem
                    key={index}
                    content={item}
                    index={index}
                    deleteItem={this.handleItemDelete}
                />
            )
        })
    }
    handleInputChange(e) {
        const value = e.target.value
        this.setState( () => ({
            inputValue: value
        }) )
    }
    handleBtnClick () {
        this.setState((prevState) => ({
            list: [...prevState.list, prevState.inputValue],
            inputValue: ''
        }))
    }
    handleItemDelete (index) {
        this.setState((prevState)=>{
            const list = [...prevState.list]
            list.splice(index, 1)
            return { list }
        })
    }
}

export default Todolist;

react编程特点

PropTypes与DefaultProps

PropTypes:对参数类型进行规定、做必填校验
DefaultProps:设置默认值


TodoItem.propTypes = {
    test: PropTypes.string.isRequired,
    content: PropTypes.string,
    deleteItem: PropTypes.func,
    index: PropTypes.number,
}

TodoItem.defaultProps = {
    test: 'hello world'
}

官方文档:https://zh-hans.reactjs.org/docs/typechecking-with-proptypes.html

(完)

上一篇 下一篇

猜你喜欢

热点阅读