pmndrs/react-nil
关于
⃝ React 空渲染器
网站与应用开发实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文Nothing to see here ...
Quite so. This package allows you to bring React's high-level component abstraction to Node or wherever you need it. Why not manage your REST endpoints like routes on the client, users as components with mount/unmount lifecycles, self-contained separation of concern, and clean side effects? Suspense for requests, etc.
You can try a small demo here: https://codesandbox.io/s/react-nil-mvpry
How does it work?
The following renders a logical component without a view, it renders nothing, but it has a real lifecycle and is managed by React regardless.
import { useState, useEffect } from 'react'
import { render } from 'react-nil'
function Foo() {
const [active, set] = useState(false)
useEffect(() => void setInterval(() => set((a) => !a), 1000), [])
// false, true, ...
console.log(active)
}
render(<Foo />)
We can take this further by rendering made-up elements that get returned as a reactive JSON tree from render.
You can take a snapshot for testing via React.act which will wait for effects and suspense to finish.
import { useState, useEffect, act } from 'react'
import { render } from 'react-nil'
declare module 'react' {
namespace JSX {
interface IntrinsicElements {
timestamp: Record<string, unknown>
}
}
}
function Test() {
const [value, setValue] = useState(-1)
useEffect(() => setValue(Date.now()), [])
return <timestamp value={value} />
}
const container = await act(async () => render(<Test />))
// { type: 'timestamp', props: { value: number }, children: [] }
console.log(container.head)
探索相关项目
加载中…