查看“︁JavaScript/react/useSelector”︁的源代码
来自康健生活
←
JavaScript/react/useSelector
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 引言 == 共享状态,从 Redux 的 store 中提取数据(state)。 <syntaxhighlight lang="javascript"> import { useSelector } from 'react-redux'; ... const { 'id': activeGroupId } = useSelector(state => state.canvas.scrollIntoView); </syntaxhighlight> {{Warning|<div>useSelector 函数内部使用 <code>===</code> 进行数值对比,需要避免不必要的新对象返回(eq: 使用 map 等)以免触发 action 导致发生重新渲染。</div>}} == 浅层比较 == <syntaxhighlight lang="javascript"> import { shallowEqual, useSelector } from 'react-redux'; ... const { 'id': activeGroupId } = useSelector(state => state.canvas.scrollIntoView, shallowEqual); </syntaxhighlight> == reselect == <syntaxhighlight lang="javascript"> import { useSelector } from 'react-redux'; import { createSelector } from 'reselect'; ... const { 'id': activeGroupId } = useSelector(createSelector( state => state.canvas.scrollIntoView, scrollIntoView => scrollIntoView.type === 'group' ));</syntaxhighlight> == 示例 == <syntaxhighlight lang="javascript"> ... const [activeGroupId, setActiveGroupId] = useState(0); const scrollIntoView = useSelector(createSelector( state => state.canvas.scrollIntoView, _ => _.type === 'group' && _ ), shallowEqual); useEffect(() => { if (!scrollIntoView) return; setActiveGroupId(scrollIntoView.id); }, [scrollIntoView]);</syntaxhighlight>
此页面嵌入的页面:
模板:Meta box
(
查看源代码
)
模板:Warning
(
查看源代码
)
模板:Warning/style.css
(
查看源代码
)
返回
JavaScript/react/useSelector
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。