第六十四周总结——React18新特性的hook

warning: 这篇文章距离上次修改已过186天,其中的内容可能已经有所变动。

React 18 引入了一些新的钩子特性,这些特性可以帮助我们更好地管理和组织我们的应用程序中的状态。

  1. useSyncExternalStore

useSyncExternalStore 是一个 React 的钩子,它可以帮助我们从外部源(例如:Redux store, RxJS subjects, or any other source)同步非React状态。




import { useSyncExternalStore } from 'use-sync-external-store/react';
import { atom } from 'jotai';
import { useAtomValue } from 'jotai/utils';
 
const countAtom = atom(0);
 
function ChildComponent() {
  const count = useAtomValue(countAtom);
  return <div>Count: {count}</div>;
}
  1. useInsertionEffect

useInsertionEffect 是一个 React 的钩子,它的行为类似于 useEffect,但它会在组件插入到 DOM 后执行,而不会导致额外的重新渲染。




import { useInsertionEffect } from 'react';
 
function ChildComponent() {
  useInsertionEffect(() => {
    // 执行一些操作,例如测量或者注册监听器
    return () => {
      // 在组件卸载时执行清理操作
    };
  }, []);
 
  return <div>Child Component</div>;
}
  1. useDeferredValue

useDeferredValue 是一个 React 的钩子,它可以用来延迟更新状态,这在需要在 DOM 更新前进行状态更新时非常有用。




import { useTransition, useDeferredValue } from 'react';
 
function ChildComponent({ value }) {
  const [isPending, startTransition] = useTransition(false);
  const deferredValue = useDeferredValue(value, { timeoutMs: 3000 });
 
  return (
    <>
      <button onClick={() => startTransition(() => setValue(value + 1))}>
        Increase
      </button>
      {isPending ? 'Pending...' : null}
      <div>Value: {deferredValue}</div>
    </>
  );
}
  1. useTransition

useTransition 是一个 React 的钩子,它可以用来创建一个过渡状态,在这个状态下,组件的更新会被延迟。




import { useTransition } from 'react';
 
function ChildComponent() {
  const [isPending, startTransition] = useTransition();
 
  return (
    <>
      <button onClick={() => startTransition(() => setValue(value + 1))}>
        Increase
      </button>
      {isPending ? 'Pending...' : null}
      <div>Value: {value}</div>
    </>
  );
}
  1. useMutableSource

useMutableSource 是一个 React 的钩子,它可以用来从可变源(如可观察对象)中读取数据。




import { useMutableSource } from 'react';
import { MutableSource } from 'react-isolate-components';
 
function ChildComponent() {
  const mutableSource = useMutableSource({
    source: myMutableSource,
    getSnapshot: myMutableSource.get,
  });
 
  return <div>Value: {mutableSource.value}</div>;
}

以上就是这周我学习到的一些新的钩子特性,它们可以帮助我们更好地管理和组织我们的应用程序中的状态。

最后修改于:2024年08月19日 22:05

评论已关闭

推荐阅读

DDPG 模型解析,附Pytorch完整代码
2024年11月24日
DQN 模型解析,附Pytorch完整代码
2024年11月24日
AIGC实战——Transformer模型
2024年12月01日
Socket TCP 和 UDP 编程基础(Python)
2024年11月30日
python , tcp , udp
如何使用 ChatGPT 进行学术润色?你需要这些指令
2024年12月01日
AI
最新 Python 调用 OpenAi 详细教程实现问答、图像合成、图像理解、语音合成、语音识别(详细教程)
2024年11月24日
ChatGPT 和 DALL·E 2 配合生成故事绘本
2024年12月01日
omegaconf,一个超强的 Python 库!
2024年11月24日
【视觉AIGC识别】误差特征、人脸伪造检测、其他类型假图检测
2024年12月01日
[超级详细]如何在深度学习训练模型过程中使用 GPU 加速
2024年11月29日
Python 物理引擎pymunk最完整教程
2024年11月27日
MediaPipe 人体姿态与手指关键点检测教程
2024年11月27日
深入了解 Taipy:Python 打造 Web 应用的全面教程
2024年11月26日
基于Transformer的时间序列预测模型
2024年11月25日
Python在金融大数据分析中的AI应用(股价分析、量化交易)实战
2024年11月25日
AIGC Gradio系列学习教程之Components
2024年12月01日
Python3 `asyncio` — 异步 I/O,事件循环和并发工具
2024年11月30日
llama-factory SFT系列教程:大模型在自定义数据集 LoRA 训练与部署
2024年12月01日
Python 多线程和多进程用法
2024年11月24日
Python socket详解,全网最全教程
2024年11月27日