Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tiny-React 难点攻克意见 #1621

Open
xiejay97 opened this issue May 11, 2024 · 1 comment
Open

Tiny-React 难点攻克意见 #1621

xiejay97 opened this issue May 11, 2024 · 1 comment

Comments

@xiejay97
Copy link
Member

xiejay97 commented May 11, 2024

  1. Provide / Inject
  2. $parent / $children

  1. 在很长一段时间,无法实现 Provide / Inject,因为 React useContext 只能顶层调用,这意味着无法在像 if 语句中获取上下文。但是目前 React Canary 版本发布了 use API,用法类似于 Provide / Inject。
  2. $parent / $children 仍然是不可实现的,但是就代码设计而言,访问父子组件本身是不可靠的,因为这依赖于开发者是否遵循组件使用的层级结构,并且破坏了代码的可读性(例如 $parent.$parent )和鲁棒性(修改组件层级导致$parent / $children的访问不可靠,直接修改父子组件属性违反了状态控制的设计模式)。我们完全可以通过上下文(Provide / Inject)或者属性(prop)的方式去实现代码需求。
@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


Title: Tiny-React Opinions on Overcoming Difficulties

  1. Provide/Inject

  2. $parent / $children

  3. 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.

  4. $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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants