React状态管理器的应用

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

javascript 复制代码
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列表不会刷新。

这时候要引入状态管理器:

javascript 复制代码
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 可以放到组件外部,带上参数即可,如下:

javascript 复制代码
...
// 将 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多很多

相关推荐
涉密IT资质笔记18 分钟前
涉密人员脱密期管理规范:期限分级模型、就业限制边界与违规认定标准
java·服务器·前端·网络·数据库
u01110267526 分钟前
工具页案例图如何编写 alt让图片说明与处理场景对应
java·前端·javascript·图像处理·人工智能·算法·ai作画
事圆则缓31 分钟前
Flutter 状态管理框架对比(五):GetX 的响应式、依赖注册与生命周期
前端·javascript·flutter
广州智造1 小时前
Radioss复合材料使用的单元属性
前端·设计·cad·建模·cae·radioss
张宏宇2 小时前
我把微软开源的 tgrep 做成了本地版 GitHub Code Search:45,629 个文件里搜一次 10ms
前端·后端
huakoh2 小时前
MCP 调用超时后别急着重试:三步读回分清执行状态
前端
打工仔折腾 AI2 小时前
把模型切换交给平台:用蓝耘智能路由搭建商品评论分析工具
java·服务器·前端·后端·python·性能优化·ai agent 实战
不可能片场2 小时前
Electron 发布标题的冒号 击穿命令行解析
前端·electron
500842 小时前
React Native for OpenHarmony 实战:三方库 react-native-crypto-js 的鸿蒙化适配指南
javascript·react native·性能优化·electron·harmonyos
reeswell2 小时前
我开源了 inspect-devtools —— 让 AI 终于能"看见"你屏幕上的组件
前端·人工智能