Why ref='string' is “legacy”?

后端 未结 2 1820
死守一世寂寞
死守一世寂寞 2020-12-13 01:34

In the React documentation they say:

React also supports using a string (instead of a callback) as a ref prop on any component, although this

2条回答
  •  Happy的楠姐
    2020-12-13 02:23

    Originally posted by danabramov on https://news.ycombinator.com/edit?id=12093234

    1. String refs are not composable. A wrapping component can’t “snoop” on a ref to a child if it already has an existing string ref. On the other hand, callback refs don’t have a single owner, so you can always compose them.
    2. String refs don’t work with static analysis like Flow. Flow can’t guess the magic that framework does to make the string ref “appear” on this.refs, as well as its type (which could be different). Callback refs are friendlier to static analysis.
    3. The owner for a string ref is determined by the currently executing component. This means that with a common “render callback” pattern (e.g. ), the wrong component will own the ref (it will end up on DataTable instead of your component defining renderRow).
    4. String refs force React to keep track of currently executing component. This is problematic because it makes react module stateful, and thus causes weird errors when react module is duplicated in the bundle.

提交回复
热议问题