Skip to content

16.4 blog post #904

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

Merged
merged 17 commits into from
May 24, 2018
Merged

16.4 blog post #904

merged 17 commits into from
May 24, 2018

Conversation

acdlite
Copy link
Member

@acdlite acdlite commented May 23, 2018

TODO:

  • Section about React.unstable_Profiler (@bvaughn) Never mind.
  • Explain why async rendering isn't in this release (?) Nah, don't need this either.
  • Update getDerivedStateFromProps docs to reflect new behavior

@reactjs-bot
Copy link

reactjs-bot commented May 23, 2018

Deploy preview for reactjs ready!

Built with commit dc0ed4b

https://deploy-preview-904--reactjs.netlify.com

@@ -0,0 +1,128 @@
---
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to change the date in the filename

@bvaughn
Copy link
Contributor

bvaughn commented May 23, 2018

Section about React.unstable_Profiler (@bvaughn)

I think we decided against this for now. We'll just mention it in the CHANGELOG (which Dan has already done).

const prevProps = state.prevProps;
// Compare incoming props to previous props
const controlledValue =
prevProps.value !== state.controlledValue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look quite right. Wouldn't this block every setState update?

// Store the previous props in state
prevProps: props,
controlledValue,
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should avoid this example. Even aside from gDSFP, this approach of half-controlled/half-uncontrolled leads to an awkward/ambiguous API.

For example, the only way to reset a half-controlled component's state to a value safely is to render twice, temporarily setting the prop to something else (e.g. null) and then setting it back.

I think instead, we should encourage fully controlled (no state) or fully uncontrolled (props.defaultValue sets initial state and never gets synced).

Maybe put another way, I think we may want to avoid a component ever updating a state attribute via setState that props also updates.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's okay we show this, since we want to cover a before and after case. I don't know. In general, I'm just concerned about any perceived encouragement of this API/design. 😄


```js
static getDerivedStateFromProps(props, state) {
if (props.value !== state.controlledValue) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth mentioning that this pattern was flawed to begin with- even setting the recent gDSFP change aside- because it would override state even when unrelated props changed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if that would be more or less confusing. It might distract from the thing you're trying to emphasize. I don't know.

controlledValue,
};
}
```
Copy link
Member

@gaearon gaearon May 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just mention here that most components don't need this pattern. We're only documenting for the cases in which you're already using it. But if you just need some value from props, you should just read it from props, and not attempt to put it into state.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example:

Note that typically the vast majority of the components don't need getDerivedStateFromProps at all. It shouldn't be used often, as making state dependent on props often leads to logic that is hard to understand. If possible, prefer to have one source of truth for any value (either in props or in state), and lift state up if it's necessary to share it between multiple components.

Copy link
Member Author

@acdlite acdlite May 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but I have mixed feelings about telling people not to use a feature, especially one we just added, unless we give them a clear alternatives. We need to follow up with a more comprehensive post soon (like this week or next).

@acdlite acdlite force-pushed the 16.4-release-post branch from 8c1a8ae to 55b2511 Compare May 23, 2018 23:45
static getDerivedStateFromProps(props, state) {
if (props.value !== state.controlledValue) {
return {
// Since this method fires on both props and state changes, local updates
Copy link
Contributor

@bvaughn bvaughn May 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super tiny nit. Please feel free to ignore.

Suggestion: Change "Since this method fires" to "Because this method fires"

(so there aren't two "since"s in the sentence)

((no idea if this is a grammatical no-no))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

Copy link
Contributor

@bvaughn bvaughn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on this 👍

@acdlite acdlite changed the title [WIP] 16.4 blog post 16.4 blog post May 24, 2018
@acdlite acdlite merged commit dfaf1ee into reactjs:master May 24, 2018
BetterZxx pushed a commit to BetterZxx/react.dev that referenced this pull request Mar 21, 2023
…md into Chinese (reactjs#904)

Co-authored-by: hexiaokang <[email protected]>
Co-authored-by: KnowsCount <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants