Here you write that render() has to return the same result every time:
The render() function should be pure, meaning that it does not modify component state, it returns the same result each time it鈥檚 invoked...
The very next part you write that render() can never return the same result:
On the next state or props update, that
render()function will return a different tree of React elements.
So is render() pure or not pure?
It returns the same result each time it's invoked _if_ you do not change its inputs, which is the props and the state. If you change the input, then it is possible that it returns a different result.
That is still a pure function, for a somewhat loose definition of pure. Pure functions are not allowed to use any state from outside of the function... such as this.props or this.state, but React encourages patterns that approximate purity as much as is reasonable. As long as render has no side effects (and only calls other pure functions, such as React.createElement), it is "pure enough" for React.
Hope the above helps.
Why should render() be pure? what is the logic behind keeping render function pure?
Most helpful comment
It returns the same result each time it's invoked _if_ you do not change its inputs, which is the props and the state. If you change the input, then it is possible that it returns a different result.
That is still a pure function, for a somewhat loose definition of pure. Pure functions are not allowed to use any state from outside of the function... such as
this.propsorthis.state, but React encourages patterns that approximate purity as much as is reasonable. As long asrenderhas no side effects (and only calls other pure functions, such asReact.createElement), it is "pure enough" for React.