From 9f6d2763efe8e0ef22cbf17c9f3f815b1664facb Mon Sep 17 00:00:00 2001 From: Sunil Pai Date: Tue, 25 Feb 2020 15:59:53 +0000 Subject: [PATCH 1/4] Blogpost for v16.13.0 --- content/authors.yml | 3 + content/blog/2020-03-02-react-v16.13.0.md | 173 ++++++++++++++++++ .../hydration-warning-after.png | Bin 0 -> 63351 bytes .../hydration-warning-before.png | Bin 0 -> 25343 bytes content/versions.yml | 2 + src/site-constants.js | 2 +- 6 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 content/blog/2020-03-02-react-v16.13.0.md create mode 100644 content/images/blog/react-v16.13.0/hydration-warning-after.png create mode 100644 content/images/blog/react-v16.13.0/hydration-warning-before.png diff --git a/content/authors.yml b/content/authors.yml index 5848c65e892..e636436c5ed 100644 --- a/content/authors.yml +++ b/content/authors.yml @@ -76,6 +76,9 @@ steveluscher: tesseralis: name: Nat Alison url: https://twitter.com/tesseralis +threepointone: + name: Sunil Pai + url: https://twitter.com/threepointone timer: name: Joe Haddad url: https://twitter.com/timer150 diff --git a/content/blog/2020-03-02-react-v16.13.0.md b/content/blog/2020-03-02-react-v16.13.0.md new file mode 100644 index 00000000000..54d9dfa6eff --- /dev/null +++ b/content/blog/2020-03-02-react-v16.13.0.md @@ -0,0 +1,173 @@ +--- +title: "React v16.13.0" +author: [threepointone] +--- + +Today we are releasing React 16.13.0. It contains bugfixes and new deprecation warnings to help prepare for a future major release. + +### Warnings for some updates during render {#warnings-for-some-updates-during-render} + +A React component should not cause side effects in other components during rendering. We do support a local update during a render to [derive state from props](/docs/hooks-faq.html#how-do-i-implement-getderivedstatefromprops). However, this only works on the same Component/Hook. If you call it during a render on a different component, this will now issue a warning. + +This warning will help you find application bugs caused by unintentional state changes. In the rare case that you intentionally want to change the state of another component as a result of rendering, you can wrap the `setState` call into `useEffect`. + +### Warnings for some deprecated string refs {#warnings-for-some-deprecated-string-refs} + +[String Refs is an old legacy API](/docs/refs-and-the-dom.html#legacy-api-string-refs) which is discouraged and is going to be deprecated in the future. This release adds a new warning to some unusual edge cases that can't be fixed automatically. For example using a string ref in a render prop: + +```jsx +class ClassWithRenderProp extends React.Component { + componentDidMount() { + something(this.refs.foo); + } + render() { + return this.props.children(); + } +} + +class ClassParent extends React.Component { + render() { + return ( + {() => } + ); + } +} +``` + +You most likely don't have code like this but if you do, we recommend that you convert it to [`React.createRef()`](/docs/refs-and-the-dom.html#creating-refs) instead: + +```jsx +class ClassWithRenderProp extends React.Component { + foo = React.createRef(); + componentDidMount() { + something(this.foo.current); + } + render() { + return this.props.children(this.foo); + } +} + +class ClassParent extends React.Component { + render() { + return ( + {foo => } + ); + } +} +``` + +In the future, we will provide an automated script (a “codemod”) to migrate away from String Refs. We are enabling this warning first so that you can fix the cases that can’t be safely converted by the codemod. + +> To get this warning you need to have the [babel-plugin-transform-react-jsx-self](https://babeljs.io/docs/en/babel-plugin-transform-react-jsx-self) installed in your Babel plugins, and it must _only_ be enabled in development mode. +> +> If you use the “react” preset you should already have this installed by default! + +### Renaming `ReactDOM.unstable_createPortal` to `ReactDOM.createPortal` {#renaming-reactdomunstable_createportal-to-reactdomcreateportal} + +When React 16 was released, `createPortal` became an officially supported API. However, we kept `unstable_createPortal` as a supported alias to keep the few libraries that adopted it working. We are now deprecating the unstable alias. Use `createPortal`directly instead. It has exactly the same signature. + +### Deprecating `React.createFactory` {#deprecating-reactcreatefactory} + +[`React.createFactory`](/docs/react-api.html#createfactory) is a legacy helper for creating React elements. This release adds a deprecation warning to the method, and will be removed in a future major version. Replace usages of `React.createFactory` with regular JSX. Alternately, you can copy and paste this one-line helper or publish it as a library: + +```jsx +let createFactory = type => React.createElement.bind(null, type); +``` + +### Component stacks in more warnings {#component-stacks-in-more-warnings} + +React adds component stacks to its development warnings, enabling developers to isolate bugs and debug their programs. This release adds component stacks to a number of development warnings that didn't previously have them. As an example, consider this hydration warning in previous versions: + +![A screenshot of the console warning, simply stating the nature of the hydration mismatch: "Warning: Expected server HTML to contain a matching div in div."](../images/blog/react-v16.13.0/hydration-warning-before.png) + +While it's pointing out an error with the code, it's not clear where the error exists, and what to do next. This release adds a component stack to this warning, which makes it look like this: + +![A screenshot of the console warning, stating the nature of the hydration mismatch, but also including a component stack : "Warning: Expected server HTML to contain a matching div in div, in div (at pages/index.js:4)..."](../images/blog/react-v16.13.0/hydration-warning-after.png) + +This makes it clear where the problem is, and lets you locate and fix the bug faster. + +### Notable bugfixes {#notable-bugfixes} + +This release contains a few other notable improvements: + +- When dynamically applying a `style` that contains longhand and shorthand versions, particular combinations of updates can cause inconsistent styles for that element. For example: + + ```jsx +
+ ... +
+ ``` + + You might expect this `
` to always have a red background, no matter the value of `toggle`. However, on alternating the value of `toggle` between `true` and `false`, you'll be surprised to see the background color start as `red`, then alternate between `transparent` and `blue` ([Try this example](https://codesandbox.io/s/suspicious-sunset-g3jub) on codesandbox). React now detects this pattern and logs a warning. The workaround is to avoid mixing both shorthand and longhand versions of a style for the same value. You can find more details on this React issue - [#6348](https://github.com/facebook/react/issues/6348) + + +- In Strict Development Mode, React calls lifecycle methods twice to flush out any possible unwanted side effects. This release adds that behaviour to `shouldComponentUpdate`. This shouldn't affect most code, unless you have side effects in `shouldComponentUpdate`. To fix this, move the code with side effects into `componentDidUpdate`. + +- In Strict Development Mode, the warnings for usage of the legacy context API didn't include the stack for the component that triggered the warning. This release adds the missing stack to the warning. + +- `onMouseEnter` now doesn't trigger on disabled `