admin管理员组文章数量:1336289
I had a form ponent with the following content
function Form() {
return (
<div className="form-container">
<div className="form-control">
<label id="text-input">Text </label>
<input type="text"></input>
</div>
<div className="form-control">
<label>Time </label>
<input type="text"></input>
</div>
<div className="form-control" style={{alignItems:"center"}}>
<button className="add-but">Add</button>
</div>
</div>
)
}
I wanted to focus the text element after the ponent gets rendered.
I was first trying to put {document.querySelector("#txt-in").focus()}
, it didn't work and after searching I found I could use the tag autoFocus
. and everything would work beautifully.
But I was wondering, what if I want to actually execute javascript code after rendering? or actually do this in javascript? I found answers for class
based ponents but couldn't find for function
based ponents like the one I am using.
how do I execute code I want executed after the element is rendred, in function based ponents?
I had a form ponent with the following content
function Form() {
return (
<div className="form-container">
<div className="form-control">
<label id="text-input">Text </label>
<input type="text"></input>
</div>
<div className="form-control">
<label>Time </label>
<input type="text"></input>
</div>
<div className="form-control" style={{alignItems:"center"}}>
<button className="add-but">Add</button>
</div>
</div>
)
}
I wanted to focus the text element after the ponent gets rendered.
I was first trying to put {document.querySelector("#txt-in").focus()}
, it didn't work and after searching I found I could use the tag autoFocus
. and everything would work beautifully.
But I was wondering, what if I want to actually execute javascript code after rendering? or actually do this in javascript? I found answers for class
based ponents but couldn't find for function
based ponents like the one I am using.
how do I execute code I want executed after the element is rendred, in function based ponents?
Share edited Mar 12, 2022 at 15:32 Abraham asked Feb 19, 2021 at 9:23 AbrahamAbraham 15.8k10 gold badges84 silver badges121 bronze badges 2-
1
You can do that using
useEffect
function if you are using react hooks orlifecycle methods
from the react class. – kunal panchal Commented Feb 19, 2021 at 9:25 - 1 For this you need a useRef hook reactjs/docs/hooks-reference.html#useref – Konstantin Samarin Commented Feb 19, 2021 at 9:25
2 Answers
Reset to default 4You can use React Hooks useEffect
for your purpose.
Simply put below code
import React, {useEffect} from "react";
function Form() {
useEffect(() => {
// Do whatever you want after first render
// Put your code here
}, [])
}
Similar to what the ponents are writing, previously one would use functions likeponentDidUpdate
& ponentDidMount
to manipulate ponents after/before being rendered. Funcitonal ponents realised we could use one 'hook' for this called useEffect
where one can trigger a particular action to occur on the basis of a state change to the ponent.
Here is a link to the docs for useEffect
- https://reactjs/docs/hooks-effect.html
版权声明:本文标题:reactjs - React run javascript code after all render is completed in function based component - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742406907a2469008.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论