V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  nav4e  ›  全部回复第 1 页 / 共 1 页
回复总数  3
@yazoox 和 ls 说的 context api 一样

|--business_context.ts
|--business page
|- page-inside
|- component-inside-inside

如在具体页面里头定好这个页面使用到的 context, 如
{
create: () => dispatch('xx'),
update: () => dispatch('xxx'),
}

之后页面内的任意级组件使用 const { create } = useContext(business_context) 去调用。

具体权衡可以看 https://zh-hans.reactjs.org/docs/context.html

context 这东西配合 ts 用起来会比较舒服
看起来主要还是为了区分 Container 和 Presentational 组件

展示组件不直接使用到 dispatch, 只响应 props 行为, 在代码大改动的时候, 只需要动到 Container
https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0

如果是出现传递层级太深, 建议定好 context 的 api, 走 context
没看注册条款直接选择了下一步
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3721 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 19ms · UTC 10:22 · PVG 18:22 · LAX 03:22 · JFK 06:22
Developed with CodeLauncher
♥ Do have faith in what you're doing.