使用Recoil与React实现规范高效Master-Details主从UI的方案
Recoil 实现高效 Master-Details 主从UI 规范方案
核心逻辑是通过状态粒度拆分避免全量重绘,核心原则是不要把所有Foo数据存在同一个大Atom中,单个数据更新只会触发订阅该数据的组件重绘。
1. 状态定义
- 用
atomFamily存储单个Foo实例,每个Foo的更新只会触发订阅对应id的组件重绘 - 用单独的Atom存储Foo的id集合,仅在新增/删除Foo时更新
- 用Atom存储当前选中的Foo id,仅切换选中项时更新
import { atom, atomFamily, selector } from 'recoil'; // 业务定义的Foo类型 type Foo = { id: string; name: string; description: string, counter: number } // 单个Foo的原子存储,key为Foo的id export const fooState = atomFamily<Foo, string>({ key: 'fooState', default: null, }); // 所有Foo的id列表,仅新增/删除时更新 export const fooIdsState = atom<string[]>({ key: 'fooIdsState', default: [], }); // 当前选中的Foo id export const selectedFooIdState = atom<string | null>({ key: 'selectedFooIdState', default: null, }); // 可选:获取当前选中的完整Foo数据的selector,简化编辑逻辑 export const selectedFooState = selector<Foo | null>({ key: 'selectedFooState', get: ({ get }) => { const selectedId = get(selectedFooIdState); if (!selectedId) return null; return get(fooState(selectedId)); }, set: ({ set }, newValue) => { if (!newValue || !('id' in newValue)) return; set(fooState(newValue.id), newValue); } });
2. 组件分层实现
左侧列表父组件
仅订阅Foo的id列表,不订阅具体Foo数据,仅在新增/删除Foo时重绘
// FooList.tsx import { useRecoilValue } from 'recoil'; import { fooIdsState } from './state'; import FooListItem from './FooListItem'; export default function FooList() { const fooIds = useRecoilValue(fooIdsState); return ( <div className="foo-list"> {fooIds.map(id => ( <FooListItem key={id} fooId={id} /> ))} </div> ) }
列表项子组件
每个子项仅订阅自身id对应的Foo数据和选中状态,仅自身数据变化、选中状态切换到/离开当前项时重绘
// FooListItem.tsx import { useRecoilValue, useSetRecoilState } from 'recoil'; import { fooState, selectedFooIdState } from './state'; interface Props { fooId: string; } // 可选:包一层React.memo,列表父组件重绘时已有子项直接跳过重绘 export default React.memo(function FooListItem({ fooId }: Props) { const foo = useRecoilValue(fooState(fooId)); const selectedId = useRecoilValue(selectedFooIdState); const setSelectedId = useSetRecoilState(selectedFooIdState); const isSelected = selectedId === fooId; return ( <div className={`foo-item ${isSelected ? 'active' : ''}`} onClick={() => setSelectedId(fooId)} > <h4>{foo.name}</h4> <p>计数:{foo.counter}</p> </div> ) })
右侧详情编辑组件
订阅选中的Foo数据,编辑时直接更新对应状态,修改会自动同步到左侧对应列表项,不会触发其他列表项重绘
// FooDetail.tsx import { useRecoilState, useResetRecoilState } from 'recoil'; import { selectedFooState, selectedFooIdState } from './state'; export default function FooDetail() { const [selectedFoo, setSelectedFoo] = useRecoilState(selectedFooState); const resetSelected = useResetRecoilState(selectedFooIdState); if (!selectedFoo) return <div>请选择左侧的Foo项</div> const handleUpdateField = (field: keyof Foo, value: any) => { setSelectedFoo({ ...selectedFoo, [field]: value }) } return ( <div className="foo-detail"> <button onClick={resetSelected}>关闭</button> <input value={selectedFoo.name} onChange={e => handleUpdateField('name', e.target.value)} placeholder="名称" /> <textarea value={selectedFoo.description} onChange={e => handleUpdateField('description', e.target.value)} placeholder="描述" /> <button onClick={() => handleUpdateField('counter', selectedFoo.counter + 1)}> 计数+1 </button> </div> ) }
3. 新增Foo功能实现
同时更新id列表和对应id的Foo实例即可,只会触发列表父组件重绘,现有列表项不会重绘
import { useSetRecoilState } from 'recoil'; import { v4 as uuidv4 } from 'uuid'; import { fooIdsState, fooState } from './state'; export function useAddFoo() { const setFooIds = useSetRecoilState(fooIdsState); const setFoo = useSetRecoilState(fooState); return (fooData: Omit<Foo, 'id'>) => { const newId = uuidv4(); const newFoo = { ...fooData, id: newId }; setFoo(newId, newFoo); setFooIds(prev => [...prev, newId]); } }
重绘控制说明
- 单个Foo编辑更新:仅触发对应id的
fooState变化,只有订阅该状态的对应列表项和详情组件重绘,其他列表项完全不会触发更新 - 新增Foo:仅
fooIdsState变化,列表父组件重绘,现有列表项的props无变化,React会直接复用现有实例不会重绘 - 切换选中项:仅
selectedFooIdState变化,只有旧选中项、新选中项和详情组件重绘,其他列表项不受影响
内容的提问来源于stack exchange,提问作者ConfusedNoob
相关产品推荐
相关产品推荐

