Answer the question
In order to leave comments, you need to log in
How to properly organize the work of the component (useEffect and API call)?
Hello, tell me how to correctly build the operation of a component with an API call.
Here is the code itself, and one of the useEffect() options, in this case, I get the old state in the render.
const [category, setCategory] = useState([]);
const {id} = useParams();
useEffect(() => {
getCategory();
}, [id]);
const getCategory = async () => {
const params = {
id: id
};
const data = await API.getOne('category', params);
setCategory(data);
}
useEffect(() => {
getCategory();
}, [category]);
useEffect(() => {
getCategory();
}, [id]);
useEffect(() => {
document.title = category.title;
}, [category.title]);
Answer the question
In order to leave comments, you need to log in
const {id} = useParams();
const [category, setCategory] = useState([]);
const getCategory = async () => {
const params = {
id: id
};
const data = await API.getOne('category', params);
setCategory(data);
}
useEffect(() => {
getCategory();
}, [id]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question