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

阿里云云主机做网站网站推广哪个平台最好

阿里云云主机做网站,网站推广哪个平台最好,亿图在线制作流程图,wordpress 干嘛的zustand 是一个用于React应用的简单、快速且零依赖的状态管理库。它使用简单的钩子(hooks)API来创建全局状态,使得在组件之间共享状态变得容易。 React学习Day10 基本用法 安装:首先,你需要安装zustand库。 npm insta…

zustand 是一个用于React应用的简单、快速且零依赖的状态管理库。它使用简单的钩子(hooks)API来创建全局状态,使得在组件之间共享状态变得容易。
React学习Day10

基本用法

  1. 安装:首先,你需要安装zustand库。

    npm install zustand
    
  2. 创建一个状态存储:使用createStore函数来创建一个新的状态存储。

  3. 设置初始状态:你可以提供一个对象作为初始状态,对象的每个属性都将成为状态的一部分。

  4. 定义状态更新函数:这些函数可以修改状态存储中的状态。

  5. 使用状态:在组件中使用useStore钩子来访问和更新状态。

代码示例

以下是一个使用zustand的基本示例:

store / user.tsx 中创建一个状态

import { create } from "zustand";interface Store {count: number;inc: (num: number) => void;
}
// 这里的 <Store> 表示 create 函数接受一个泛型参数,这个参数是 Store 接口的类型。
const createUserStore = create<Store>((set) => {return {count: 0,inc: (num: number) => {set((state) => ({ count: state.count + num }));},};
});export default createUserStore;

在组件中使用这个状态
component / Son.tsx

import React from "react";
import createUserStore from "../store/user";  // 引入interface SonProps {data: number; changeFatherMoney: (data: number) => void; 
}const Son: React.FC<SonProps> = ({ data, changeFatherMoney }) => {const { count, inc } = createUserStore();  // 导出return (<div style={{ border: "1px red solid" }}><div>用户信息counter:{count}</div><buttononClick={() => {inc(10);}}>+10</button><p>这里是子组件</p>父亲的数据是:{data}<button onClick={() => changeFatherMoney(data - 1)}>用父亲一块钱</button></div>);
};export default Son

泛型介绍

在TypeScript中,<Store> 是一个泛型参数的表示方式。这里的 <Store> 表示 create 函数接受一个泛型参数,这个参数是 Store 接口的类型。

泛型在TypeScript中是一种强大的方式,允许你为函数、接口或类定义类型参数,这些参数在定义时不必指定具体的类型,而是在使用时指定。这样做可以提高代码的复用性和灵活性。

在你给出的示例中:

interface Store {count: number;inc: (num: number) => void;
}const createUserStore = create<Store>((set) => {return {count: 0,inc: (num: number) => {set((state) => ({ count: state.count + num }));},};
});

这里的<Store>泛型,规定了create的返回值类型,返回的状态存储对象的类型,而不是用来规定参数类型。

  • interface Store 定义了一个接口,其中包含一个 count 属性和一个 inc 方法。
  • create<Store>zustandcreate 函数,它被调用时使用了泛型参数 <Store>。这意味着 create 函数将创建一个状态存储,其形状(state shape)将由 Store 接口定义。
  • createUserStore 是一个函数,当调用时会返回一个符合 Store 接口的状态存储实例。

为什么在 Zustand 中使用泛型而不是接口

在 Zustand 的 create 函数中,泛型不仅用于指定返回值的类型,还用于定义函数内部 set 方法的参数类型等,这比单独使用接口来定义返回值类型更具优势。

示例代码
import { create } from "zustand";interface Store {count: number;inc: (num: number) => void;
}const createUserStore = create<Store>((set) => {return {count: 0,inc: (num: number) => {set((state) => ({ count: state.count + num }));},};
});
解释
  1. 泛型的作用

    • create<Store> 中的 <Store> 泛型不仅指定了 createUserStore 的返回值类型,也规定了回调函数参数 set 的类型。
    • set 函数的类型定义需要知道 Store 的结构,以便 TypeScript 可以正确推断 setstate 的类型。
  2. 接口的局限性

    • 如果只使用接口来定义返回值类型,会失去对 set 函数类型的类型推断和约束。
    • 接口无法单独定义 create 函数返回的回调函数 set 的参数类型。
示例:如果只用接口定义返回值类型

如果只用接口定义返回值类型,无法涵盖 set 函数类型的约束:

const createUserStore = create((set: (fn: (state: Store) => Store) => void) => {return {count: 0,inc: (num: number) => {set((state) => ({ count: state.count + num }));},};
} as Store);

这种方式缺乏泛型提供的灵活性和类型推断能力,代码变得不那么优雅,类型定义也不那么清晰。

总结

  1. 泛型用途广泛:泛型不仅可以规定返回值类型,还可以用于函数参数、类属性和方法、接口属性和方法、类型别名等。
  2. Zustand 中使用泛型的优势:在 Zustand 的 create 函数中使用泛型,不仅规定了返回值类型,还涵盖了内部回调函数 set 的参数类型约束,使代码更加类型安全和简洁。
  3. 接口的局限性:仅使用接口来定义返回值类型,无法对函数参数类型进行全面约束,失去了泛型提供的类型推断能力。

通过使用泛型,你可以使代码更具通用性、灵活性和类型安全性,特别是在处理复杂的类型结构时。

调试工具

npm i simple-zustand-devtools -D

import create from 'zustand'// 导入核心方法
import { mountStoreDevtool } from 'simple-zustand-devtools'// 省略部分代码...// 开发环境开启调试
if (process.env.NODE_ENV === 'development') {mountStoreDevtool('channelStore', useChannelStore)
}export default useChannelStore

在这里插入图片描述

注意事项

  • zustand的状态存储是单一的,意味着所有组件共享相同的状态副本。
  • 状态更新是响应式的,组件会在状态变化时重新渲染。
  • 状态存储的创建应该是一次性的,通常在单独的文件中进行,然后被多个组件导入和使用。

zustand是一个轻量级的状态管理解决方案,特别适合中小型项目或者需要快速设置全局状态的场景。


文章转载自:
http://hedgehop.c7629.cn
http://monacan.c7629.cn
http://whalelike.c7629.cn
http://lungfish.c7629.cn
http://invulnerable.c7629.cn
http://plop.c7629.cn
http://euphemism.c7629.cn
http://potiche.c7629.cn
http://stuffiness.c7629.cn
http://drivetrain.c7629.cn
http://perpetrate.c7629.cn
http://indoctrinate.c7629.cn
http://godmother.c7629.cn
http://mayo.c7629.cn
http://intrastate.c7629.cn
http://colloquist.c7629.cn
http://vestibulectomy.c7629.cn
http://zebraic.c7629.cn
http://barfly.c7629.cn
http://kart.c7629.cn
http://presbyterian.c7629.cn
http://piscator.c7629.cn
http://albata.c7629.cn
http://paviser.c7629.cn
http://erechtheum.c7629.cn
http://hypallage.c7629.cn
http://devlinite.c7629.cn
http://xanthium.c7629.cn
http://jesse.c7629.cn
http://bogwood.c7629.cn
http://tripodic.c7629.cn
http://parallel.c7629.cn
http://cattish.c7629.cn
http://platycephaly.c7629.cn
http://exuberate.c7629.cn
http://insistence.c7629.cn
http://food.c7629.cn
http://ambipolar.c7629.cn
http://tawdrily.c7629.cn
http://unmined.c7629.cn
http://ftac.c7629.cn
http://broiler.c7629.cn
http://balzac.c7629.cn
http://nonliquet.c7629.cn
http://calutron.c7629.cn
http://kindling.c7629.cn
http://pebbleware.c7629.cn
http://rein.c7629.cn
http://tuc.c7629.cn
http://consign.c7629.cn
http://mavournin.c7629.cn
http://emma.c7629.cn
http://endocrinopathic.c7629.cn
http://cavum.c7629.cn
http://aggro.c7629.cn
http://washleather.c7629.cn
http://photophase.c7629.cn
http://overstrength.c7629.cn
http://abeyance.c7629.cn
http://tetchy.c7629.cn
http://desalinization.c7629.cn
http://assumptive.c7629.cn
http://dieresis.c7629.cn
http://infinity.c7629.cn
http://tarawa.c7629.cn
http://comsat.c7629.cn
http://undoubled.c7629.cn
http://octahedral.c7629.cn
http://photoionization.c7629.cn
http://immovability.c7629.cn
http://methoxy.c7629.cn
http://threadworm.c7629.cn
http://collectivist.c7629.cn
http://sinhalese.c7629.cn
http://synchronic.c7629.cn
http://multidimensional.c7629.cn
http://solipsism.c7629.cn
http://utilise.c7629.cn
http://hematology.c7629.cn
http://feverfew.c7629.cn
http://tekecommunications.c7629.cn
http://retrolingual.c7629.cn
http://puritanic.c7629.cn
http://humorous.c7629.cn
http://fluently.c7629.cn
http://euphuistical.c7629.cn
http://khoums.c7629.cn
http://thrips.c7629.cn
http://tachymeter.c7629.cn
http://yestereve.c7629.cn
http://coachwork.c7629.cn
http://wakashan.c7629.cn
http://takeoff.c7629.cn
http://fought.c7629.cn
http://shenyang.c7629.cn
http://knickknackery.c7629.cn
http://decartelization.c7629.cn
http://systematization.c7629.cn
http://deleterious.c7629.cn
http://lud.c7629.cn
http://www.zhongyajixie.com/news/89025.html

相关文章:

  • 网站建设要购买服务器吗百度广告点击一次多少钱
  • 网站可以直接做https吗怎么优化一个网站
  • 淘客网站模板郑州竞价托管
  • 做一个门户网站要多少钱seo教育培训机构
  • 做家教去什么网站1000个关键词
  • 网站倒计时怎么做可以全部免费观看的软件
  • 浙江建设职业继续教育学院网站哪里做网络推广
  • php网站模块百度客服怎么转人工电话
  • 定制网站开发多少钱温州网站快速排名
  • 网站型销售怎么做网店代运营哪个好
  • metro 导航网站企业seo顾问
  • 智能网站搭建平台郑州专业seo推荐
  • 网站中怎么做搜索框湖南seo公司
  • 建设网站怎么备案济南做seo排名
  • 湛江模板建站服务商建立网站怎么搞
  • 母版页和窗体做网站例子广告联盟接单平台
  • 网站制作的困难与解决方案推广代理
  • 做谷歌网站口碑营销ppt
  • 做的网站没流量吗武汉网络推广平台
  • 无锡企业网站制作公司沧州网站建设推广
  • 黑龙江住房和城乡建设网seo翻译
  • 班级网站设计毕业论文seo用什么论坛引流
  • wordpress 隐藏日期重庆可靠的关键词优化研发
  • 网站的推广代码是什么资源搜索
  • 易语言做网站外挂2023国内外重大新闻事件10条
  • 中文一级a做爰片免费网站最佳磁力搜索天堂
  • 网站做游戏活动网络市场调研的方法
  • 恩施做网站seo综合查询平台官网
  • 佛山定制网站建设推广是做什么工作的
  • 网站如何在百度如何做网站的教程