• React之UI组件(将render部分的JSX模板拆分出来)和容器组件 (拆分JSX后剩余的逻辑部分)


    UI组件

    // TodoListUI.js
    
    import React, { Component } from 'react'
    import 'antd/dist/antd.css'
    import { Input, Button, List } from 'antd'
    
    class TodoListUI extends Component {
      render () {
        return (
          <div style={{marginTop: '10px', marginLeft: '10px'}}>
            <div>
              <Input
                value={this.props.inputValue}  
                placeholder='todo info' 
                style={{ '300px', marginRight: '10px'}} 
                onChange={this.props.handleInputChange}
              />
              <Button type="primary" onClick={this.props.handleBtnClick}>提交</Button>
            </div>
            <List
              style={{marginTop: '10px',  '300px'}}
              bordered
              dataSource={this.props.list}
              renderItem={(item, index) => (<List.Item onClick={(index) => {this.props.handleItemDelete(index)}} >{item}</List.Item>)}
            />
          </div>
        )
      }
    }
    
    export default TodoListUI
    
    

    容器组件

    // TodoList.js
    
    import React, { Component } from 'react'
    import store from './store'
    import {getInputChangeAction , getAddItemAction, getDeleteItemAction } from './store/actionCreator'
    import TodoListUI from './TodoListUI'
    
    class TodoList extends Component {
      constructor (props) {
        super(props)
        this.state = store.getState()
        console.log(this.state )
        this.handleInputChange = this.handleInputChange.bind(this)
        this.handleStoreChange = this.handleStoreChange.bind(this)
        this.handleBtnClick = this.handleBtnClick.bind(this)
        this.handleItemDelete = this.handleItemDelete.bind(this)
        store.subscribe(this.handleStoreChange)
      }
    
      componentDidMount () {
        console.log(1111111)
      }
    
      render () {
        return (
          <TodoListUI 
            inputValue = {this.state.inputValue}
            list = {this.state.list}
            handleInputChange = {this.handleInputChange}
            handleBtnClick = {this.handleBtnClick}
            handleItemDelete = {this.handleItemDelete}
          />
        )
      }
    
      handleInputChange (e) {
        // const action = {
        //   type: CHANGE_INPUT_VALUE,
        //   value: e.target.value
        // }
        // 将action拆分出去通过函数获取
        const action = getInputChangeAction(e.target.value)
        console.log(e.target.value)
        store.dispatch(action)
      }
    
      handleStoreChange () {
        console.log('store_change')
        this.setState(store.getState())
      }
    
      handleBtnClick () {
        // const action = {
        //   type: ADD_TODO_ITEM
        // }
        const action = getAddItemAction()
        store.dispatch(action)
      }
    
      handleItemDelete (index) {
        // const action = {
        //   type: DELETE_TODO_ITEM,
        //   index
        // }
        const action = getDeleteItemAction(index)
        store.dispatch(action)
        // alert(index)
      }
    }
    
    export default TodoList
    
    
    今天你学习了吗!!!
  • 相关阅读:
    互动留言赠书:《Oracle高性能系统实战大全》
    超融合硬件损坏导致Oracle RAC异常恢复实录
    架构师不得不了解的硬件知识
    加班做的可视化被老板嫌弃,是因为你不会用这些数据工具
    iOS开发之七:常用控件--UISlider、UISegmentedControl、UIPageControl的使用
    iOS开发之六:常用控件--UIImageView的使用
    iOS开发之五:常用控件--UITextField的使用
    Objective-C实现常用的4种排序算法
    C语言实现4种常用排序
    面试常用的4种数组排序
  • 原文地址:https://www.cnblogs.com/nayek/p/12383115.html
Copyright © 2020-2023  润新知