当前位置: 首页 > news >正文

React状态管理器的应用

Vue的响应能力,直接改变data()里面字段的值UI就会自动刷新,而react不会,如下代码:

import React from 'react';
import { Badge, TabBar, Button } from 'antd-mobile';
import {
  ScanningOutline,
  AppOutline,
  MessageOutline,
  MessageFill,
  UnorderedListOutline,
  UserOutline,
} from 'antd-mobile-icons';



const Menu = () => {
  let products=[
    { title: '卷心菜', isFruit: false, id: 1 },
    { title: '大蒜', isFruit: false, id: 2 },
    { title: '苹果', isFruit: true, id: 3 }
  ];

  const ShoppingList = () => {
    return (
      <ul>
        {products.map(i => (
          <li key={i.id} style={{ color: i.isFruit ? 'magenta' : 'darkgreen' }}>
            {i.title}
          </li>
        ))}
      </ul>
    );
  };
  return (
    <div style={{ padding: 20 }}>
      <h2>菜单页面</h2>
      <ShoppingList />
      <div>
        <Button type="primary" onClick={() => {
          const productName = prompt('请输入产品名称');
          if (productName) {
            const newProduct = {
              title: productName,
              isFruit: false,
              id: products.length + 1
            };
            products=[...products, newProduct];
          }
        }}>添加11</Button>
      </div>
    </div>
  );
};
export default Menu;

直接改变products,界面中的products列表不会刷新。
这时候要引入状态管理器:

import React, { useState } from 'react';
import { Badge, TabBar, Button } from 'antd-mobile';
import {
  ScanningOutline,
  AppOutline,
  MessageOutline,
  MessageFill,
  UnorderedListOutline,
  UserOutline,
} from 'antd-mobile-icons';



const Menu = () => {
  const [products, setProducts] = useState([
    { title: '卷心菜', isFruit: false, id: 1 },
    { title: '大蒜', isFruit: false, id: 2 },
    { title: '苹果', isFruit: true, id: 3 }
  ]);

  const ShoppingList = () => {
    return (
      <ul>
        {products.map(i => (
          <li key={i.id} style={{ color: i.isFruit ? 'magenta' : 'darkgreen' }}>
            {i.title}
          </li>
        ))}
      </ul>
    );
  };
  return (
    <div style={{ padding: 20 }}>
      <h2>菜单页面</h2>
      <ShoppingList />
      <div>
        <Button type="primary" onClick={() => {
          const productName = prompt('请输入产品名称');
          if (productName) {
            const newProduct = {
              title: productName,
              isFruit: false,
              id: products.length + 1
            };
            setProducts([...products, newProduct]);
          }
        }}>添加</Button>
      </div>
    </div>
  );
};
export default Menu;

需要注意的是useState要在组件内部使用!ShoppingList 可以放到组件外部,带上参数即可,如下:

...
// 将 ShoppingList 函数移到组件外部
const ShoppingList = (props) => {
  const { args } = props;
  return (
    <ul>
      {args.map(i => (
        <li key={i.id} style={{ color: i.isFruit ? 'magenta' : 'darkgreen' }}>
          {i.title}
        </li>
      ))}
    </ul>
  );
};

const Menu = () => {
  const [products, setProducts] = useState([
    { title: '卷心菜', isFruit: false, id: 1 },
    { title: '大蒜', isFruit: false, id: 2 },
    { title: '苹果', isFruit: true, id: 3 }
  ]);

  return (
    <div style={{ padding: 20 }}>
      <h2>菜单页面</h2>
      <ShoppingList args={products} />
      <div>
        <Button type="primary" onClick={() => {
          const productName = prompt('请输入产品名称');
          if (productName) {
            const newProduct = {
              title: productName,
              isFruit: false,
              id: products.length + 1
            };
            setProducts([...products, newProduct]);
          }
        }}>添加</Button>
      </div>
    </div>
  );
};
export default Menu;

吐槽一下,React代码量要比vue多很多

相关文章:

  • Python小练习系列 Vol.6:单词搜索(网格回溯)
  • 测试cursor编辑器
  • Java基础 3.29
  • C++11·部分重要语法II
  • android11关机安卓充电的UI定制化
  • 练习题:110
  • Mybatis逆向工程
  • 【商城实战(94)】构建高并发的负载均衡与集群架构
  • RedHatLinux(2025.3.22)
  • 解决 macOS (M1 Pro) 上使用 Vite 进行 Build 打包时 Node 进程内存溢出的问题
  • 复现GitHub上`https://github.com/tobiasfshr/map4d`这个项目
  • Android学习总结之ContentProvider跨应用数据共享
  • 无需docker三步安装deepseek可视化操作软件-Open-WebUI
  • RabbitMQ消息相关
  • #C8# UVM中的factory机制 #S8.5# 对factory机制的重载进一步思考(二)
  • Hyperlane:Rust Web开发的未来,释放极致性能与简洁之美
  • 2025-3-29算法打卡
  • epoll 和ractor模型学习
  • Docker 的实质作用是什么
  • Blender多摄像机怎么指定相机渲染图像
  • 五一假期上海地铁部分线路将延时运营,这些调整请查收
  • 国务院安委会对辽宁辽阳一饭店重大火灾事故查处挂牌督办
  • 直播电商行业代表呼吁:携手并肩伸出援手助力外贸企业攻坚克难
  • 长三角铁路“五一”假期运输今启动:预计发送旅客量增6%,5月1日当天有望创新高
  • 港理大公布多项AI+医工成果,助港建设国际医疗创新枢纽
  • 央行副行长:我们在研究丰富政策工具箱,将适时推出增量政策