0

I have this problem and I hope that you'll be able to help me. So, I've created a React Hook to fetch data from my MySQL database. The code written for the hook is presented below:

const useMySQLToView = (table) => {
    const [rows, setRows] = useState([])

    useEffect(() => {
        async function fetchData() {
            const result = await axios.get("http://localhost:4999/get"+table)
            setRows(result)
        }
        fetchData()
    }, [table])

    return rows
}

Then I tried to get data using the hook on my component:

function ViewEmployees() {
    const {employees} = useMySQLToView('Employees')
    console.log(employees)
    return (
        <div>
            View employees
        </div>
    )
}

The problem is that this console.log(employees) returns undefined. Do you know why? How can I fix this? Thank you!

1
  • your employees state should update when data is available from the api Commented Mar 28, 2021 at 19:21

1 Answer 1

1

Replace const {employees} = useMySQLToView('Employees') with

const employees = useMySQLToView('Employees')

Since your hook is simply returning rows and not an object you can destructure

Sign up to request clarification or add additional context in comments.

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.