英文:
How many times render will actually run in a React Component
问题
I was going through React LifeCycle methods when I got stumbled on this:
I got confused as I am seeing render() function to run 2 times. All I know is that any function in React Life-Cycle runs only once. So, why am I seening 2 render functions here (or running 2 times). Won't it affect the memory and overuse for running 2nd time.
Also, How we know where render function would run (or at what stage) as it can run at 2 places in React Cycle. Kinldy, help clarify.
Reference:
https://gist.github.com/bvaughn/923dffb2cd9504ee440791fade8db5f9
英文:
I was going through React LifeCycle methods when I got stumbled on this:
I got confused as I am seeing render() function to run 2 times. All I know is that any function in React Life-Cycle runs only once. So, why am I seening 2 render functions here (or running 2 times). Won't it affect the memory and overuse for running 2nd time.
Also, How we know where render function would run (or at what stage) as it can run at 2 places in React Cycle. Kinldy, help clarify.
Reference:
https://gist.github.com/bvaughn/923dffb2cd9504ee440791fade8db5f9
答案1
得分: 3
> Mounting
首先,Reactjs只会渲染一次render
方法,生命周期如下:
constructor();
static getDerivedStateFromProps()
render();
componentDidMount();
> Updating
但是,当你更新组件状态或接收到新的props时,将会触发以下生命周期:
static getDerivedStateFromProps()
shouldComponentUpdate();
render();
getSnapshotBeforeUpdate();
componentDidUpdate();
请注意,从shouldComponentUpdate()
返回false将不会触发渲染。
除了render()
之外的所有方法都是可选的。
英文:
> Mounting
At first, Reactjs will render method only once and the lifecycle would be:
constructor();
static getDerivedStateFromProps()
render();
componentDidMount();
> Updating
But as you update component state or on receiving new props
will trigger
the following lifecycle:
static getDerivedStateFromProps()
shouldComponentUpdate();
render();
getSnapshotBeforeUpdate();
componentDidUpdate();
note that, returning false from shouldComponentUpdate()
will not trigger render
All methods except render()
are optional
答案2
得分: 2
对于一个组件,render()
函数显然可以在同一个挂载上运行多次。您可以参考React文档中的这个表格。
从表格中可以清楚地看出,如果一个组件已经挂载,只有 constructor
和 componentDidMount
会运行一次(不包括 componentWillUnmount
,它也会在组件卸载时运行一次),而其他生命周期方法可以根据组件的更新次数运行多次。
英文:
For a component, the render()
function obviously can run more than once for the same mount. You may refer to this table from the React docs.
From the table, it's clearly that if a component is mounted, only constructor
and componentDidMount
will run once (excluding componentWillUnmount
which also run once when the component is unmounted), while the other lifecycle methods can run unlimited times, depends on the number of updates of that component.
答案3
得分: 1
简短回答是,每当状态或属性的值发生更改时,您的渲染方法将运行,除非您通过从生命周期方法中返回false来强制停止,正如Navin Gelot所提到的那样,否则它将不会运行渲染方法。
英文:
Short answer is that whenever a value of state or prop will change,your render method will run until and unless you are force stopping by returning false from of the life cycle method as @Navin Gelot mentioned,then it will not run render method otherwise yes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论