admin管理员组文章数量:1327849
I'm working on a search function with algolia search. The user may put in a search term from a different site, get redirected to the search page, and the search should be invoked.
After this, to search for more stuff, the user need to use the search bar in the search page, and hit enter to invoke the search again.
I've tried to use a useState and set it to true after the first render. However when I type in my search bar, the useEffect gets invoked at every key press. Why is this happening when firstRender doesn't change? It will set firstRender to true again, but since firstRender already is true, nothing should happen?
How can I "deactivate" the useEffect after the first render, so that the user only uses the search bar to search?
(I don't want to hammer the API with requests while the user is typing)
Thanks!
const SearchRecipes = () => {
const client = algoliasearch(process.env.REACT_APP_ALGOLIA_APP_ID, process.env.REACT_APP_ALGOLIA_SEARCH_API);
const index = client.initIndex('recipe_search');
const match = useRouteMatch();
const [searchTerm, setSearchTerm] = useState(match.params.id);
const [searchReturnData, setSearchReturnData] = useState([]);
const [firstRender, setFirstRender] = useState(false);
const useMountEffect = (fun) => useEffect(fun, [])
useEffect(() => {
handleSubmit();
setFirstRender(true);
}, [firstRender])
const handleSubmit = (e = null) => {
if (e !== null){
e.preventDefault();
}
index
.search(searchTerm)
.then((responses) => {
setSearchReturnData(responses.hits);
});
}
return (
// irrelevant JSX
<div className="keywords">
<input
type="text"
value={searchTerm}
onChange={({target}) => setSearchTerm(target.value)}
/>
</div>
// more irrelevant JSX
)
}
I'm working on a search function with algolia search. The user may put in a search term from a different site, get redirected to the search page, and the search should be invoked.
After this, to search for more stuff, the user need to use the search bar in the search page, and hit enter to invoke the search again.
I've tried to use a useState and set it to true after the first render. However when I type in my search bar, the useEffect gets invoked at every key press. Why is this happening when firstRender doesn't change? It will set firstRender to true again, but since firstRender already is true, nothing should happen?
How can I "deactivate" the useEffect after the first render, so that the user only uses the search bar to search?
(I don't want to hammer the API with requests while the user is typing)
Thanks!
const SearchRecipes = () => {
const client = algoliasearch(process.env.REACT_APP_ALGOLIA_APP_ID, process.env.REACT_APP_ALGOLIA_SEARCH_API);
const index = client.initIndex('recipe_search');
const match = useRouteMatch();
const [searchTerm, setSearchTerm] = useState(match.params.id);
const [searchReturnData, setSearchReturnData] = useState([]);
const [firstRender, setFirstRender] = useState(false);
const useMountEffect = (fun) => useEffect(fun, [])
useEffect(() => {
handleSubmit();
setFirstRender(true);
}, [firstRender])
const handleSubmit = (e = null) => {
if (e !== null){
e.preventDefault();
}
index
.search(searchTerm)
.then((responses) => {
setSearchReturnData(responses.hits);
});
}
return (
// irrelevant JSX
<div className="keywords">
<input
type="text"
value={searchTerm}
onChange={({target}) => setSearchTerm(target.value)}
/>
</div>
// more irrelevant JSX
)
}
Share
Improve this question
asked Jul 28, 2020 at 4:43
Stephan Bakkelund ValoisStephan Bakkelund Valois
1,0722 gold badges13 silver badges25 bronze badges
1
- 1 Trye UseEffect(()=>{//your code},[]), it will be invoked only for the first render – Asutosh Commented Jul 28, 2020 at 4:46
1 Answer
Reset to default 4The reason is that your useEffect
has a dependency on firstRender
and inside it it is setting that value.
You could either follow the suggestion from the ment, using an empty array of dependencies:
useEffect(() => {
handleSubmit();
}, [])
or check if your firstRender
state is already set:
useEffect(() => {
if (!firstRender) {
handleSubmit();
setFirstRender(true);
}
}, [firstRender])
This is a really nice article about useEffect
if you need more info.
本文标签: javascriptReact how to run a function only onceafter page enter or refreshStack Overflow
版权声明:本文标题:javascript - React how to run a function only once, after page enter or refresh - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742215044a2434415.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论