-
Notifications
You must be signed in to change notification settings - Fork 4k
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
docs(ComponentExample): replace deprecated lifecycle methods #3533
docs(ComponentExample): replace deprecated lifecycle methods #3533
Conversation
Wow, that's great ❤️ |
Codecov Report
@@ Coverage Diff @@
## master #3533 +/- ##
=======================================
Coverage 99.81% 99.81%
=======================================
Files 174 174
Lines 2725 2725
=======================================
Hits 2720 2720
Misses 5 5 Continue to review full report at Codecov.
|
docs/src/components/ComponentDoc/ComponentExample/ComponentExample.js
Outdated
Show resolved
Hide resolved
location: { hash }, | ||
} = nextProps | ||
|
||
const anchorName = examplePathToHash(examplePath) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can move anchorName
to constructor()
as we need it only once
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can do that only in case if examplePath
property is never changed. Current usage of ComponentExample
implies such usage. But it is a little bit confusing from general perspective.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on nature of ComponentExample
it's impossible in production and almost impossible in development, so actually we can do this :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. I got it. Another reason to have anchorName
in state is the ability to use it in static context during derived state computation. If the point for having it as a class field is to perform a computation just once I can do a conditional assignment to the non-empty anchorName.
docs/src/components/ComponentDoc/ComponentExample/ComponentExample.js
Outdated
Show resolved
Hide resolved
constructor(props) { | ||
super(props) | ||
|
||
const state = ComponentExample.getDerivedStateFromProps(props) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getDerivedStateFromProps()
will be dispatcher right after constructor()
, we don't need this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use computation for state here to be able to enable source code displaying and set initial value for source code (see an assignment below).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand what you mean, but I prefer separation of concerns there: if we will change something in getDerivedStateFromProps()
it will break constructor.
Other confusion is placed around lifecycle: https://code.likeagirl.io/understanding-react-component-life-cycle-49bf4b8674de (we call constructor()
, getDerivedStateFromProps()
and then again getDerivedStateFromProps()
)
const originalSourceCode = props.exampleSources[props.examplePath]
this.state = {
anchorName: examplePathToHash(props.examplePath),
originalSourceCode,
showCode: anchorName === props.location.hash,
sourceCode: originalSourceCode,
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can just remove line n93 (...state,
).
docs/src/components/ComponentDoc/ComponentExample/ComponentExample.js
Outdated
Show resolved
Hide resolved
@grumblerchester I've pushed some changes and merged |
@layershifter Hey! I've checked the code locally. Everything works fine |
…c-Org#3533) * docs(ComponentExample): replace deprecated lifecycle methods * updates * add `hashName`
Relates to #2732: Removes
componentWillMount
andcomponentWillReceiveProps
fromdocs/src/components/ComponentDoc/ComponentExample/ComponentExample.js
.