美文网首页
15.UI组件和容器组件19-05-28

15.UI组件和容器组件19-05-28

作者: 你坤儿姐 | 来源:发表于2019-05-28 11:04 被阅读0次

将Todolist.js中render的代码进行封装
创建TodoListUI.js将原来Todolist.js中render的UI组件代码进行封装
Todolist中原来的代码如下

render() {
       return (
           <div style={{margin:'10px'}}>
              <div>
                <Input
                  value={this.state.inputValue}
                  placeholder='todo info'
                  style={{width:'300px', marginRight:'10px'}}
                  onChange={this.handleInputChange}
                  />
                <Button type="primary" onClick={this.handleBtnClick}>提交</Button>
              </div>
             <List
               style={{marginTop:'10px',width:'300px'}}
               bordered
               dataSource={this.state.list}
               renderItem={(item, index) => <List.Item onClick={this.bandleItemDelete.bind(this, index)}>{item}</List.Item>}
             />
           </div>
       )
     }

TodoListUI.js

import React, {Component} from "react";
import {Button, Input, List} from "antd";

class TodoListUI extends Component{
  render() {
    return  <div style={{margin: '10px'}}>
      <div>
        <Input
          value={this.props.inputValue}
          placeholder='todo info'
          style={{width: '300px', marginRight: '10px'}}
          onChange={this.props.handleInputChange}
        />
        <Button type="primary" onClick={this.props.handleBtnChange}>提交</Button>
      </div>
      <List
        style={{marginTop: '10px', width: '300px'}}
        bordered
        dataSource={this.props.list}
        renderItem={(item, index) => (<List.Item onClick={(index) => {this.props.bandleItemDelete(index)}}>{item}</List.Item>)}
      />
    </div> }
}
export default TodoListUI;

Todolist.js

render() {
    return (
     <TodoListUI
       inputValue={this.state.inputValue}
       list={this.state.list}
       handleInputChange={this.handleInputChange}
       handleBtnChange={this.handleBtnChange}
       bandleItemDelete={this.bandleItemDelete}
     />
    )
  }

当一个组件只有一个render函数的时候,可以通过一个无状态组件替换掉这个普通的组件,无状态组件相对于普通组件性能比较高。

TodoListUI.js

import React, {Component} from "react";
import {Button, Input, List} from "antd";

const TodoListUI = (props)=> {
  return (
    <div style={{margin: '10px'}}>
      <div>
        <Input
          value={props.inputValue}
          placeholder='todo info'
          style={{width: '300px', marginRight: '10px'}}
          onChange={props.handleInputChange}
        />
        <Button type="primary" onClick={props.handleBtnChange}>提交</Button>
      </div>
      <List
        style={{marginTop: '10px', width: '300px'}}
        bordered
        dataSource={props.list}
        renderItem={(item, index) => (<List.Item onClick={(index) => {
          props.bandleItemDelete(index)
        }}>{item}</List.Item>)}
      />
    </div>
  )
}
export default TodoListUI;

相关文章

  • 15.UI组件和容器组件19-05-28

    将Todolist.js中render的代码进行封装创建TodoListUI.js将原来Todolist.js中r...

  • 07.Redux进阶(上)

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

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

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

  • Vue之合理划分容器组件与展示组件

    组件的职能划分 如果要将 Vue 组件按照职能划分,我们可以将其分为两种类型:容器组件和展示组件。 容器组件和展示...

  • react学习笔记-UI组件、容器组件、Redux进阶(7)

    6-1、UI组件和容器组件 UI组件:负责页面渲染 容器组件:处理页面逻辑 6-2、无状态组件 一个普通组件只有r...

  • 微信小程序(四)视图组件

    小程序组件 视图容器 (一) 视图容器 了解小程序组件中的视图容器 明确小程序组件中的视图容器的用途和方法 了解不...

  • 组件和容器

    组件和组件容器 我们可以通过一个例子来说明组件和组件容器的作用,比如下面的始终的例子。 我们可以看到click这个...

  • 用好React,你必须要知道的事情

    容器性组件(container component)和展示性组件(presentational component...

  • react-router

    安装 示例 容器组件 react-router的容器组件,就是最外层包括所有路由的组件,所有路由活动需要在容器组件...

  • react-redux connect方法

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

网友评论

      本文标题:15.UI组件和容器组件19-05-28

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