admin管理员组文章数量:1303451
i have a child ponent which emits an action to the parent ponent with a event:
Child ponent:
export default function ChildComponent(props) {
const classes = useStyles();
const [value, setValue] = React.useState([0, 5]);
const handleChange = (_, newValue) => {
setValue(newValue);
props.updateData(newValue)
};
return (
<div className={classes.root}>
<GrandSonComponent
value={value}
onChange={handleChange}
/>
</div>
);
}
Parent ponent:
export const ParentComponent = () => {
const [loading, setLoading] = React.useState(true);
const { appData, appDispatch } = React.useContext(Context);
function fetchElements(val) {
fetchData(val);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => { return fetchData() }, []);
async function fetchData(params) {
const res = await axios.get('/url', { params });
appDispatch({ type: "LOAD_ELEMENTS", elements: res.data });
}
return (
<div>
<ChildComponent updateData={fetchElements} />
<div>
.
.
.
)
};
i would like to know how to remove this line // eslint-disable-next-line react-hooks/exhaustive-deps
I need to add this line, because otherwise i see the eslint error:
React Hook useEffect has a missing dependency: 'fetchData'. Either include it or remove the
dependency array.eslintreact-hooks/exhaustive-deps
i need to use fetchData(params)
function for the first time page is rendered and when user change/click value of child ponent with no eslit warnings!
Thanks!
i have a child ponent which emits an action to the parent ponent with a event:
Child ponent:
export default function ChildComponent(props) {
const classes = useStyles();
const [value, setValue] = React.useState([0, 5]);
const handleChange = (_, newValue) => {
setValue(newValue);
props.updateData(newValue)
};
return (
<div className={classes.root}>
<GrandSonComponent
value={value}
onChange={handleChange}
/>
</div>
);
}
Parent ponent:
export const ParentComponent = () => {
const [loading, setLoading] = React.useState(true);
const { appData, appDispatch } = React.useContext(Context);
function fetchElements(val) {
fetchData(val);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => { return fetchData() }, []);
async function fetchData(params) {
const res = await axios.get('/url', { params });
appDispatch({ type: "LOAD_ELEMENTS", elements: res.data });
}
return (
<div>
<ChildComponent updateData={fetchElements} />
<div>
.
.
.
)
};
i would like to know how to remove this line // eslint-disable-next-line react-hooks/exhaustive-deps
I need to add this line, because otherwise i see the eslint error:
React Hook useEffect has a missing dependency: 'fetchData'. Either include it or remove the
dependency array.eslintreact-hooks/exhaustive-deps
i need to use fetchData(params)
function for the first time page is rendered and when user change/click value of child ponent with no eslit warnings!
Thanks!
Share Improve this question asked Nov 14, 2020 at 17:30 1pct1pct 651 gold badge1 silver badge6 bronze badges 4-
1
This doesn't answer your specific question, but
useEffect(() => { return fetchData() }, []);
is not how you would call the function the first time it renders. When you return from a use effect, it will assume it's a cleanup function, in this case to run on unmount.fetchData
also returns a promise, so it would break when it tries to call it. Remove thereturn
keyword. – Brian Thompson Commented Nov 14, 2020 at 17:38 - Same result, does not fix the issue! – 1pct Commented Nov 14, 2020 at 17:40
- This doesn't answer your specific question - it wasn't supposed to. It was pointing out a second problem in your code. – Brian Thompson Commented Nov 14, 2020 at 17:41
-
You can use
useRef
for the same. stackoverflow./a/70255826/8494462 – Shivam Sharma Commented Dec 7, 2021 at 6:37
1 Answer
Reset to default 7First of all, you don't need to return the result of calling fetchData()
function inside the useEffect
hook.
Now ing to your problem, reason why you get a warning is because missing the dependencies of the useEffect
could lead to bugs due to closures. React remends that you don't omit any dependencies of the useEffect
hook, useCallback
hook, etc.
This sometimes leads to infinite loop of state update and re-render but that can be prevented by either using useCallback
hook or other ways that could prevent useEffect
hook from executing after each re-render of the ponent.
In your case, you could fix the problem by following the steps mentioned below:
Wrapping
fetchData
function in auseCallback
hookconst fetchData = useCallback(async (params) => { const res = await axios.get('/url', { params }); appDispatch({ type: "LOAD_ELEMENTS", elements: res.data }); }, []);
Add
fetchData
in the dependency array of theuseEffect
hookuseEffect(() => { fetchData(); }, [fetchData]);
本文标签: javascriptcall function from useEffect and other functionStack Overflow
版权声明:本文标题:javascript - call function from useEffect and other function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741759657a2396326.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论