-1

When I remove , []) dependencies it's okay, but creating an infinite loop - with it, I have this error and other elements are not working properly.

However, how can I fix it? I can't figure out why am I getting the error below:

Thanks

Line 74:8:  React Hook useEffect has a missing dependency: 'database'. Either include it or remove the dependency array. You can also do a functional
update 'setDatabase(d => ...)' if you only need 'database' in the 'setDatabase' call  react-hooks/exhaustive-deps

function AddOrder(props) {
    const [step, setStep] = useState(1)
    const [redirect, setRedirect] = useState(false)

    const [database, setDatabase] = useState({
        clients: [],
        orders: [],
        client: [],
        products: [],
        subProducts: [],
        filteredSubProducts: [],


useEffect(() => {

        axios.get('http://127.0.0.1:8000/api/clients')
            .then(res => {
                setDatabase({...database, clients: res.data})
            })
        axios.get('http://127.0.0.1:8000/api/orders')
            .then(res => {
                setDatabase({...database, orders: res.data})
            })
        axios.get('http://127.0.0.1:8000/api/products')
            .then(res => {
                setDatabase({...database, products: res.data})
            })
        axios.get('http://127.0.0.1:8000/api/subProducts')
            .then(res => {
                setDatabase({...database, subProducts: res.data})
            })
    }, []);
3
  • Does this answer your question? stackoverflow.com/questions/55938884/… Commented Jul 6, 2020 at 19:36
  • not really, they have [term], I don't pass anything. and I couldn't find the similarity - I would love to try any suggestions Commented Jul 6, 2020 at 19:51
  • If you add [database] to useEffect the warning goes away Commented Jul 6, 2020 at 20:00

2 Answers 2

2

I would take the warning's suggestion and use the functional update form of setDatabase:

function AddOrder(props) {
    const [step, setStep] = useState(1)
    const [redirect, setRedirect] = useState(false)

    const [database, setDatabase] = useState({
        clients: [],
        orders: [],
        client: [],
        products: [],
        subProducts: [],
        filteredSubProducts: [],
    });

    useEffect(() => {
        axios.get('http://127.0.0.1:8000/api/clients')
            .then(res => {
                setDatabase(database => ({...database, clients: res.data}))
            })
        axios.get('http://127.0.0.1:8000/api/orders')
            .then(res => {
                setDatabase(database => ({...database, orders: res.data}))
            })
        axios.get('http://127.0.0.1:8000/api/products')
            .then(res => {
                setDatabase(database => ({...database, products: res.data}))
            })
        axios.get('http://127.0.0.1:8000/api/subProducts')
            .then(res => {
                setDatabase(database => ({...database, subProducts: res.data}))
            })
    }, []);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I think it works, thanks - I'll check it through and will update an answer. thanks again!
0

Alternative solution to Josh Wilson's: Including database in the dependency array.

function AddOrder(props) {
    const [step, setStep] = useState(1)
    const [redirect, setRedirect] = useState(false)

    const [database, setDatabase] = useState({
        clients: [],
        orders: [],
        client: [],
        products: [],
        subProducts: [],
        filteredSubProducts: [],
    });

    useEffect(() => {
         axios.get('http://127.0.0.1:8000/api/clients')
            .then(res => {
                setDatabase({...database, clients: res.data})
            })
        axios.get('http://127.0.0.1:8000/api/orders')
            .then(res => {
                setDatabase({...database, orders: res.data})
            })
        axios.get('http://127.0.0.1:8000/api/products')
            .then(res => {
                setDatabase({...database, products: res.data})
            })
        axios.get('http://127.0.0.1:8000/api/subProducts')
            .then(res => {
                setDatabase({...database, subProducts: res.data})
            })
    }, [database]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.