美文网首页
React中UI组件与容器组件的拆分

React中UI组件与容器组件的拆分

作者: 雪燃归来 | 来源:发表于2020-06-20 20:39 被阅读0次

       本篇文章,我们接着对React组件进行优化。React中存在UI组件容器组件的概念。顾名思义,UI组件主要控制组件的显示,并不是与过多的逻辑耦合,容器组件中实现一些具体的逻辑,引入UI组件作为其子组件,将子组件(UI组件)需要的一些数据通过组件间传值的方式方式传递到UI组件,进行数据的渲染。
       下面,我们就对我们之前实现TodoList的应用拆分。如果您没有关注过之前的代码,也没有关系。相信通过我上面的说明,你已经能搞清楚我上面所说的呢!

容器组件TodoList.js

import React, { Component } from 'react'
import store from './store'
import { getInputChangeAction, getAddItemAction, getDeleteItemAction } from './store/actionCreator'
import 'antd/dist/antd.css';
import TodoListUI from './TodoListUI'

class TodoList extends Component{
    constructor(props){
        super(props)
        this.state = {
            list: [],
            inputValue: ''
        }
        this.setState({
            list: store.getState().list,
            inputValue: store.getState().inputValue
        })
        this.handleInputChange = this.handleInputChange.bind(this)
        this.handleStoreChange = this.handleStoreChange.bind(this)
        this.handleAddItem = this.handleAddItem.bind(this)
        store.subscribe(this.handleStoreChange)
    }
    render(){
        return (
            <TodoListUI
                value={this.state.inputValue}
                list={this.state.list}
                handleInputChange={this.handleInputChange}
                handleAddItem={this.handleAddItem}
                handleItemDelete={this.handleItemDelete}/>
        )
    }
    handleStoreChange(){
        this.setState(store.getState())
    }
    handleInputChange(e){
        const action = getInputChangeAction(e.target.value)
        store.dispatch(action)
    }
    handleAddItem(){
        const action = getAddItemAction()
        store.dispatch(action)
    }
    handleItemDelete(index){
        const action = getDeleteItemAction(index)
        store.dispatch(action)
    }
}
export default TodoList

       上面这段代码的重点是下面的这段代码。

<TodoListUI
       value={this.state.inputValue}
       list={this.state.list}
       handleInputChange={this.handleInputChange}
       handleAddItem={this.handleAddItem}
       handleItemDelete={this.handleItemDelete}/>

UI组件

import React, { Component } from 'react'
import { Input, Button, List } from 'antd';
import 'antd/dist/antd.css';

class TodoListUI extends Component{
    render(){
        return (
            <div style={{marginTop:'10px',marginLeft:'10px'}}>
                <div>
                    <Input 
                    value={this.props.inputValue} 
                    placeholder="TodoList" 
                    onChange={this.props.handleInputChange}
                    style={{width:'300px'}}/>
                    <Button 
                    onClick={this.props.handleAddItem}
                    type="primary">提交</Button>
                </div>
                <List
                    style={{marginTop:'10px',width:'300px'}}
                    size="small"
                    bordered
                    dataSource={this.props.list}
                    renderItem={(item) => <List.Item onClick={(index) => {this.props.handleItemDelete(index)}}>{item}</List.Item>}
                    />
            </div>
        )
    }
}

export default TodoListUI

       如果一个组件中只有render函数,那么这个组件我们称之为无状态组件。无状态组件可以改写成函数形式。

import React from 'react'
import { Input, Button, List } from 'antd';
import 'antd/dist/antd.css';

const TodoListUI = (props) => {
    return (
        <div style={{marginTop:'10px',marginLeft:'10px'}}>
            <div>
                <Input 
                value={props.inputValue} 
                placeholder="TodoList" 
                onChange={props.handleInputChange}
                style={{width:'300px'}}/>
                <Button 
                onClick={props.handleAddItem}
                type="primary">提交</Button>
            </div>
            <List
                style={{marginTop:'10px',width:'300px'}}
                size="small"
                bordered
                dataSource={props.list}
                renderItem={(item) => <List.Item onClick={(index) => {props.handleItemDelete(index)}}>{item}</List.Item>}
                />
        </div>
    )
}

export default TodoListUI

相关文章

  • React中UI组件与容器组件的拆分

    本篇文章,我们接着对React组件进行优化。React中存在UI组件和容器组件的概念。顾名思义,UI组件主要控制组...

  • 【学习笔记 】React ⑧ react-redux的使用

    UI组件、容器组件、无状态组件 在学习react-redux之前,需要了解UI组件、容器组件和无状态组件的知识。 ...

  • React中UI组件和容器组件的拆分

    UI组件,也叫傻瓜组件,负责页面的渲染。 容器组件,也叫聪明组件,负责处理逻辑。 拿todolist组件举例,to...

  • react-redux connect方法

    1.组件 react-redux把组件分为两类:UI组件和容器组件。 UI组件的特征: 只负责 UI 的呈现,不带...

  • React-Redux

    UI 组件和容器组件 React-Redux 将所有组件分为两大类:UI ( presentational com...

  • (二十三)UI组件与容器组件的拆分

    UI组件-页面渲染容器组件-页面逻辑 拆分这样的todolist 把页面渲染从 src\TodoList.js 中...

  • RN:React-Redux

    目录一. React-Redux是什么 1. React-Redux的组件拆分规范  1.1 UI组件  1.2 ...

  • react-redux

    react-redux 主要: react-redux与redux并不完全一样 主要特征 ui组件 容器组件由 R...

  • 07.Redux进阶(上)

    UI组件和容器组件 在这个例子中,我们想要分离UI组件和容器组件 无状态组件 因为上面TodoListUI这个组件...

  • React 容器组件与UI组件

    react 官方推出的 react-redux 插件对构建 react 项目中的 redux 进行了优化。 Rea...

网友评论

      本文标题:React中UI组件与容器组件的拆分

      本文链接:https://www.haomeiwen.com/subject/wzugxktx.html