Brightleaf React Hooks
Useful React Hooks
useFetch
- Hook to use FetchuseGet
- Hook to make a get requestusePost
- Hook to make an HTTP PostuseClickOutside
- Hook to handle a click outside an elementuseGraphQL
- Hook to make a GraphQL requestuseNes
- Hook to connect to Hapijs NESuseWebSockets
- Hook to interact with WebSockets
GraphQL Hook
import React, { useState } from 'react'
import { useGraphQL } from '@brightleaf/hooks'
export default () => {
const [id, setId] = useState(1)
const { data, loading, error } = useGraphQL(
'https://swapiql.herokuapp.com/graphql',
`query Person($id: ID) {
person(id: $id) {
id,
name
}
}`,
{ id }
)
if (loading) {
return <div>Loading Data</div>
}
if (error) {
return <div>Error getting graphql data</div>
}
return <div>{data.person[0].name}</div>
}
Post Hook
import React from 'react'
import { Button, Form, TextBox } from 'react-form-elements'
import { usePost } from '@brightleaf/hooks'
export default () => {
const { data, error, loading, postData } = usePost(
`https://kev-pi.herokuapp.com/api/echo`
)
if (loading) {
return <div>Loading Data</div>
}
if (error) {
return <div>Error getting graphql data</div>
}
return (
<Form
name="testForm"
onSubmit={data => {
postData(data)
}}
>
<TextBox label="My Label" name="myTextBox" />
<Button>Send</Button>
</Form>
)
}
Get Hook
import React, { useState } from 'react'
import { useGet } from '@brightleaf/react-hooks'
export default () => {
const [id, setId] = useState(1)
const { data, error, loading } = useGet(
`https://swapiql.herokuapp.com/api/characters/${id}`
)
if (error) {
return (
<div className="App">
<h1>Error Getting Data</h1>
</div>
)
}
if (loading) {
return (
<div className="App">
<h1>Loading Data</h1>
</div>
)
}
return (
<div className="App">
<h1>{data[0].name}</h1>
<h2>Pick a number</h2>
<div className="select">
<div
onClick={e => {
setId(2)
}}
>
2
</div>
</div>
</div>
)
}