Khan / react-components

khan.github.io/react-components/
MIT License
1.01k stars 99 forks source link

[TeX] Why did you use componentDidUpdate instead of componentWillReceiveProps to render a Node? #46

Closed kwangkim closed 9 years ago

kwangkim commented 9 years ago

In your TeX component, you used componentDidUpdate to update katex DOMs. But I think componentWillReceiveProps is a better choice as iamdanfox did in react-katex.

Could you give a hint why you used componentDidUpdate? Thanks.

sophiebits commented 9 years ago

(I'm on the React core team.) componentDidUpdate is a better choice for updating the DOM because componentWillReceiveProps is right in the middle of the update cycle and makes it easy to cause DOM thrash causing slow perf. When possible, do all DOM reads (for layout/style especially) in componentWillUpdate and writes in componentDidMount.

Stylistically, I prefer to reserve componentWillReceiveProps for updating state before the upcoming render anyway, as that's why the hook was added. componentDidUpdate also has the advantage that this.props has the "correct" (new) value.

kwangkim commented 9 years ago

Now I could understand why componentDidUpdate is a better choice. Since I needed to render it on the server side, I decided to use katex.renderToString instead. Thank you very much for your explanation.

sophiebits commented 9 years ago

That's a good option too. At the time this component was written, that method wasn't available.