Khan/react-components

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

Closed this issue · 3 comments

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.

(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.

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.

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