博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
react-hooks 实现简单的评论list
阅读量:7233 次
发布时间:2019-06-29

本文共 1543 字,大约阅读时间需要 5 分钟。

react 16.7.0-alpha.2版本中推出了新版本的react-hook,详细的可以参照, 下面我讲用react-hooks实现简单的评论list

效果图:

demo整体架构使用react 16.8 + webpack + antd-mobile

源代码:

  • 创建输入框hooks
    const [list, setList] = useState([{
    name: 'mark'}, {
    name: 'joan'}]); const [value, setValue] = useState('');
    setValue(value)} />
    复制代码
  • Button
    const publish = () => {    setList(list.concat([{name:value}]));    setValue(''); } 复制代码
  • list
const removeTodo = index => {        const todoList = [...list];        todoList.splice(index,1);        setList(todoList);      }     
    {list.map((item,index) =>
  • {item.name} removeTodo(index)}>删除
  • )}
复制代码

完整代码

import React, { useState, useEffect} from 'react';import {Button, InputItem, List} from 'antd-mobile';const Home = () => {  const [list, setList] = useState([{
name: 'mark'}, {
name: 'joan'}]); const [value, setValue] = useState(''); const removeTodo = index => { const todoList = [...list]; todoList.splice(index,1); setList(todoList); } const publish = () => { setList(list.concat([{
name:value}])); setValue(''); } useEffect(()=>{ console.log('list变化了'); }, [list]) return (
setValue(value)} />
    {list.map((item,index) =>
  • {item.name} removeTodo(index)}>删除
  • )}
)}复制代码

这里只用了useState 和 useEffect 也是最常用的两个hook ,当然远远不止这两个hook, 大家可以参考官方文档查看更多。

转载于:https://juejin.im/post/5c8670c2e51d45206776888b

你可能感兴趣的文章
axd与ashx区别
查看>>
[翻译]你或许还未听说过的一些ASP.NET 2.0要诀
查看>>
浅谈对象的深度拷贝 - IClonable接口
查看>>
【大型网站技术实践】初级篇:借助LVS+Keepalived实现负载均衡
查看>>
资源的引用
查看>>
PeopleRank从社交网络中发现个体价值
查看>>
最优化
查看>>
三茗闪电恢复
查看>>
一起谈.NET技术,NGuestBook架构体系及实现指南
查看>>
我的hibernate学习笔记(之三)
查看>>
消息队列
查看>>
64 位 win7 使用PLSQL Developer
查看>>
按这六项做,电脑慢下来都难
查看>>
如何启用 FILESTREAM
查看>>
UIImageView 与 UIImage 区别
查看>>
用spirng和hessian构建分布式应用(远程接口)的方法(2)
查看>>
架构之美第四章-架构与美
查看>>
VirtualBox 安装增强工具
查看>>
Linux 文件管理(C语言库函数二--程序日志)
查看>>
020——VUE中变异方法push的留言版实例讲解
查看>>