mirror of
https://github.com/reactjs/react.dev.git
synced 2026-02-25 23:05:23 +00:00
Added string ref section to strict mode page
This commit is contained in:
@@ -15,7 +15,7 @@ You can enable strict mode for any part of your application. For example:
|
||||
In the above example, strict mode checks will *not* be run against the `Header` and `Footer` components. However, `RouteOne` and `RouteTwo`, as well as all of their descendants, will have the checks.
|
||||
|
||||
In version 16.3, `StrictMode` helps with:
|
||||
* Identifying components with unsafe lifecycles
|
||||
* [Identifying components with unsafe lifecycles](#identifying-unsafe-lifecycles)
|
||||
* Warning about legacy string ref API usage
|
||||
* Detecting unexpected side effects
|
||||
|
||||
@@ -31,6 +31,21 @@ When strict mode is enabled, React compiles a list of all class components using
|
||||
|
||||
Addressing the issues identified by strict mode _now_ will make it easier for you to take advantage of async rendering in future releases of React.
|
||||
|
||||
### Warning about legacy string ref API usage
|
||||
|
||||
Previously, React provided two ways for managing refs: the legacy string ref API and the callback API. Although the string ref API was the more convenient of the two, it had [several downsides](https://github.com/facebook/react/issues/1373) and so our official recomendation was to [use the callback form instead](https://reactjs.org/docs/refs-and-the-dom.html#legacy-api-string-refs).
|
||||
|
||||
Version 16.3 adds a new option for managing refs that offers the convenience of a string ref without any of the downsides:
|
||||
`embed:16-3-release-blog-create-ref.js`
|
||||
|
||||
> **Note:**
|
||||
>
|
||||
> Callback refs will continue to be supported in addition to the new `createRef` API.
|
||||
>
|
||||
> You don't need to replace callback refs in your components. They are slightly more flexible, so they will remain as an advanced feature.
|
||||
|
||||
[Learn more about the new `createRef` API here.](#)
|
||||
|
||||
### Detecting unexpected side effects
|
||||
|
||||
As a general rule, side-effects should be avoided in certain class component methods (e.g. the `constructor`, `render`, etc). This is because React may invoke these methods more than once before committing, or it may invoke them without committing at all (because of an error or a higher priority interruption). Ignoring this rule can lead to a variety of problems, including memory leaks and invalid state. Unfortunately, it can be difficult to detect these problems as they are often non-deterministic.
|
||||
|
||||
19
examples/16-3-release-blog-create-ref.js
Normal file
19
examples/16-3-release-blog-create-ref.js
Normal file
@@ -0,0 +1,19 @@
|
||||
class MyComponent extends React.Component {
|
||||
// highlight-next-line
|
||||
divRef = React.createRef();
|
||||
|
||||
render() {
|
||||
// highlight-range{4}
|
||||
return (
|
||||
<input
|
||||
type="text"
|
||||
ref={this.divRef}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
// highlight-next-line
|
||||
this.divRef.value.focus();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user