useConstructor is a custom hook for do something before the first component render
npm install --save use-constructor-hook
import React, { useState } from 'react'
import { useConstructor } from 'use-constructor-hook'
import { fetchDataFromApi } from './api.js'
const App = () => {
const [dataFetched, setDataFetched] = useState(undefined)
useConstructor(async () => {
const response = await fetchDataFromApi()
})
return (
<div>{dataFetched || 'Loading data...'}<div>
)
}
Submit an issue
- 🐦 Elvis Gastelum - Author