tiny-vue
tiny-vue copied to clipboard
Tiny-React 难点攻克意见
- Provide / Inject
- $parent / $children
- 在很长一段时间,无法实现 Provide / Inject,因为 React useContext 只能顶层调用,这意味着无法在像
if语句中获取上下文。但是目前 React Canary 版本发布了 use API,用法类似于 Provide / Inject。 - $parent / $children 仍然是不可实现的,但是就代码设计而言,访问父子组件本身是不可靠的,因为这依赖于开发者是否遵循组件使用的层级结构,并且破坏了代码的可读性(例如
$parent.$parent)和鲁棒性(修改组件层级导致$parent / $children的访问不可靠,直接修改父子组件属性违反了状态控制的设计模式)。我们完全可以通过上下文(Provide / Inject)或者属性(prop)的方式去实现代码需求。
Bot detected the issue body's language is not English, translate it automatically.
Title: Tiny-React Opinions on Overcoming Difficulties
-
Provide/Inject
-
$parent / $children
-
For a long time, Provide / Inject could not be implemented because React useContext could only be called at the top level, which meant that the context could not be obtained in statements like
if. However, the current React Canary version has released the use API, whose usage is similar to Provide / Inject. -
$parent / $children are still unimplementable, but in terms of code design, accessing the parent-child component itself is unreliable, because it relies on the developer following the hierarchical structure used by the component, and destroys the readability of the code (e.g.
$parent.$parent) and robustness (modifying the component hierarchy causes unreliable access to $parent/$children, and directly modifying parent-child component properties violates the design pattern of state control). We can completely implement code requirements through context (Provide/Inject) or attributes (prop).