ScrollView Custom Scroll Indicator
Custom Scroll Indicator for ScrollView
1 | import React, { useMemo, useState } from "react" |
1 | import React, { useMemo, useState } from "react" |
Set 类似于数组,但是成员的值都是唯一的,没有重复。Set 本身是一个构造函数。
1 | const set = new Set() |
在 Set 内部,NaN 是相等的,两个对象总是不相等的。
1 | let set = new Set() |
网络上对 react hooks 的评价负面大于正面,确实很容易写出性能有问题的代码,关键就在于:我们太喜欢用 useState 了。
在 vue-composition-api 中,reactivity 数据都有 wrapper,custom-vca 里不管产生多少个 reactivity 对象,不会直接产生 re-render。只有那些被 return 到外部跟 template 绑定的部分才会触发视图渲染。
而 react 的 reactivity 就是通过 re-render 实现的,useState 没有 wrapper,每次使用都会得到一个触发渲染的函数。在这种 reactivity 机制下,就需要特殊的方式编写 hooks —— State/Effect 分层
假设有个 useHeight:
1 | const [ref, height] = useHeight() |
高度变化时,被动 re-render,难以转换合并。大部分情况下,不提供 state,而提供 effect 可能会更好:
1 | const [height, setHeight] = useState(0) |
使用者在外部声明 state,然后在 callback 中按需 setState。使用者可以结合其他 state,做 dispatch 到 reducer 的一次整体更新,而不是被动 re-render。
根据 State/Effect 分层理念,尝试着给出友好地 react hooks 公式:
1 | const handler = useProducer(consumer, options) |
producer 接收 consumer callback 作为参数,返回 handler 控制函数,用于绑定到事件或其他位置。
I’ve recorded an MP4 demo to showcase the results. In the video, you’ll see the Markdown content being displayed
character by character, with dynamic rendering and automatic scrolling.
In this article, I’ll walk you through a simple yet powerful demo of implementing stream output in JavaScript. This
demo
mimics the behavior of ChatGPT-like applications, where responses are displayed in a streaming fashion, and the
content
is dynamically rendered as Markdown. The best part? It’s incredibly simple to implement, and I’ll provide the full
source code and an MP4 demo to showcase the results.