A place for notes on React changes Iβm keeping an eye on.
useEffect and useSo from my understanding thereβs a new hook and some changes to useEffect. These are the notes Iβve gathered from various different posts on LinkedIn and Twitter. Iβm not very good at keeping up with React proposals on Github; I tend to rely on newsletters and tweets.
Firstly, theyβre introducting async in server components. Now from what Iβve gathered over the last couple of years, this has been something thatβs been in the works for a while. Theyβve mentioned it being in beta. I quite like this and the example below looks pretty neat.
// The current way of doing things
const ExampleComponent = () => {
const [data, setData] = useState([])
useEffect(() => {
fetch('url').then((res) => setData(res.json))
}, [])
}
return(
<div>
{
data?.map(example => (
<p>example.name</p>
))
}
</div>
)
// The new way of doing things
const ExampleComponent = async() => {
const data = fetch('url').then((res) => res.json)
return (
{data.map(example => (
<p>example.name</p>
))}
)
}
The other thing is a new hook called use. So essentially this can replace await and unwrap the value of a promise. Oh, and itβs the only conditional React hook, the first of itβs kind! So this is very new from what I have gathered. It can be used nested inside conditions, blocks and loops; this means no more splitting the logic up, or potentially React-Query(?!). With use you can set conditions to wait for data to load.
I hear there have been documentation changes to useEffect, Iβm yet to read these but this is a small reminder to myself.
If you want to read more about the things mentioned above click here.