setState in useEffect loops the application

Why am I getting infinite loop error? React refers to the line marked with *:

function Blog() {
const [blog, setBlog] = useState({});
const query = useQuery();

async function takeBlog(query) {
const _data = await (await fetch(`$…


This content originally appeared on DEV Community and was authored by

Why am I getting infinite loop error? React refers to the line marked with *:

function Blog() {
    const [blog, setBlog] = useState({});
    const query = useQuery();

    async function takeBlog(query) {
        const _data = await (await fetch(`${root}/api/blog/${query}`)).json();
        console.log(_data.blog)
        setBlog(_.get(_data, 'blog', {}));  // *
    }

    useEffect(() => {
        takeBlog(query);
    }, []);

    return (
        <div className="blog_page">
            <div className="container">
                <div className="top">
                </div>
                <div className="other ab">
                    <div className="left">
                        <Navbar active='blogs' />
                    </div>
                    <div className="right">
                        <Posts posts={_.get(blog, 'articles', [])} />
                    </div>
                </div>
            </div>
        </div>
    );
}


This content originally appeared on DEV Community and was authored by


Print Share Comment Cite Upload Translate Updates
APA

| Sciencx (2021-04-28T09:54:21+00:00) setState in useEffect loops the application. Retrieved from https://www.scien.cx/2021/04/28/setstate-in-useeffect-loops-the-application/

MLA
" » setState in useEffect loops the application." | Sciencx - Wednesday April 28, 2021, https://www.scien.cx/2021/04/28/setstate-in-useeffect-loops-the-application/
HARVARD
| Sciencx Wednesday April 28, 2021 » setState in useEffect loops the application., viewed ,<https://www.scien.cx/2021/04/28/setstate-in-useeffect-loops-the-application/>
VANCOUVER
| Sciencx - » setState in useEffect loops the application. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/28/setstate-in-useeffect-loops-the-application/
CHICAGO
" » setState in useEffect loops the application." | Sciencx - Accessed . https://www.scien.cx/2021/04/28/setstate-in-useeffect-loops-the-application/
IEEE
" » setState in useEffect loops the application." | Sciencx [Online]. Available: https://www.scien.cx/2021/04/28/setstate-in-useeffect-loops-the-application/. [Accessed: ]
rf:citation
» setState in useEffect loops the application | | Sciencx | https://www.scien.cx/2021/04/28/setstate-in-useeffect-loops-the-application/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.