-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Refactoring Markdown lifecycle methods to React 16.3 #19436
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
Refactoring Markdown lifecycle methods to React 16.3 #19436
Conversation
💚 Build Succeeded |
ppisljar
left a comment
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.
the only comment i have is that the new code is harder to read than the old one, looking at the issues you mentioned it seems others are coming to the same conclusion ....
|
@ppisljar Yes, it's a bit harder to read, and we are duplicating props into state. I can get rid of the initialization of markdown in the constructor since Other possible solutions are:
|
4e14ea8 to
54dfe78
Compare
54dfe78 to
d55dc42
Compare
💚 Build Succeeded |
src/ui/public/markdown/markdown.js
Outdated
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 would suggest, we just use a pure function component instead inheriting PureComponent here. Functional of course no difference, but I think it reads a bit cleaner and aligns better with what we usually do in components.
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'm using a PureComponent to take advantage of its shallow compare of props. This avoids calling the render method if the parent rerenders and the props aren't changed as opposed to what happens when using a pure function that renders every time its parent renders independently from the props and state.
We have the markdownit render method in the render method of the pure component so it's better to don't rerender everything every time.
src/ui/public/markdown/markdown.js
Outdated
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 think we should rather return only the rendered markdown from this function, and wrap it in { __html } in the component below. That way the vanilla JS function stays less related to React.
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 will do ;)
ppisljar
left a comment
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.
LGTM, tested on chrome-linux
d55dc42 to
96537a7
Compare
💚 Build Succeeded |
nreese
left a comment
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.
lgtm
code review
tested markdown rendering on home page "add data"
src/ui/public/markdown/markdown.js
Outdated
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.
Typo "Rhe" should be "The"
96537a7 to
de3c3f8
Compare
💚 Build Succeeded |
de3c3f8 to
8624cc3
Compare
💚 Build Succeeded |
8624cc3 to
eb4a633
Compare
💚 Build Succeeded |
eb4a633 to
1e3a0ea
Compare
💚 Build Succeeded |
* Removed componentWillReceiveProps * Memoized markdown factory * Refactored some tests
* Removed componentWillReceiveProps * Memoized markdown factory * Refactored some tests
This PR updates the Markdown component to new React 16.3 lifecycle methods as requested in #17432.
It removes completely the
componentWillReceivePropsmethod in favour of a memoized markdownFactory method. ThemarkdownFactorynow usewhiteListedRulesandopenLinksInNewTabas the cache key and return a callable function for rendering a markdown string (avoiding a possible state change of the markdown-it instance).The memoized function is used by the markdown vis and the tutorial components, so a maximum of 3 instances will coexist on Kibana.
Markdown is now a PureComponent.