I'm trying to create an API Service for fetching data in all my components. I'm stuck with Typescript (i'm not really good with it)
I've created a custom hook:
useApiService.tsx
import React from 'react'
import axios from 'axios'
const useApiService = (url: string) => {
const baseUrl = 'http://localhost:8009'
const [response, setResponse] = React.useState(null)
const [error, setError] = React.useState(null)
React.useEffect(() => {
const fetchData = async (): Promise<void> => {
try {
const res = await axios(`${baseUrl}${url}`)
setResponse(res.data)
} catch (error) {
setError(error)
}
}
fetchData()
}, [url])
return { response, error }
}
export default useApiService
and then use this hook in my components like so :
Header.tsx
import React from 'react'
import { Button, Form, FormControl, Navbar, NavItem } from 'react-bootstrap'
import Nav from 'react-bootstrap/Nav'
import { Link } from '@reach/router'
import useApiService from '../services/useApiService'
interface NavigationData {
title: string
ID: string
post_excerpt: string
}
interface SiteData {
name: string
}
const Header: React.FC = () => {
const menuQuery: Array<NavigationData> = useApiService('/wp-json/wp-utils/menus')
const siteNameQuery: SiteData = useApiService('/wp-json')
const data = menuQuery.response
const siteName = siteNameQuery.response
if (data && siteName)
return (
<Navbar bg="primary" expand="lg" className="navbar-dark mb-4">
<Link to={'/'}>
<Navbar.Brand>{siteName.name}</Navbar.Brand>
</Link>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="mr-auto">
{data.map((item: NavigationData) => (
<NavItem key={item.ID}>
<Link className="nav-link" to={item.post_excerpt}>
{item.title}
</Link>
</NavItem>
))}
</Nav>
<Form className={'my-2 my-lg-0'} inline>
<FormControl type="text" placeholder="Recherche" className="mr-sm-2" />
<Button variant="light" className="my-2 my-sm-0">
Search
</Button>
</Form>
</Navbar.Collapse>
</Navbar>
)
else return null
}
export default Header
But I have typescript errors Type '{ response: null; error: null; }' is missing the following properties from type 'NavigationData[]': length, pop, push, concat, and 28 more.
I would like to inject the data interface or type from my component to my custom hook but I don't know if it's the good way to do this, and how to do this right.
Thanks for your advices