Fast is real. Free is not ❯ I want to write an article about this: AI like you is excellent at accelerating development, but it…
Practical thinking on software engineering.
Fast is real. Free is not ❯ I want to write an article about this: AI like you is excellent at accelerating development, but it…
Have you ever been into a situation where you wanted to do something with your state inside useEffect hook but didn’t want to put it into a dependency array?
react-hooks/exhaustive-deps is an excellent guard when working with primitive values, but as soon as you have an object in the state, it might stay on your way.
Let me show you some code, which requires state handling in the way I’ve described above.
type FetchInfo =
| { state: 'LOADING' }
| { state: 'LOADED' }
| { state: 'ERROR'; error: Error; statusCode: number | undefined }
| { state: 'INITIAL' }
interface StateType {
fetchInfo: FetchInfo
input: string | undefined
value: Value
}
export const VerySpecialComponent: React.FC<{ input: string }> = ({ input }) => {
const [state, setState] = React.useState<StateType>({
fetchInfo: { state: 'INITIAL' },
input: undefined,
value: undefined,
})
React.useEffect(() => {
if (state.fetchInfo.state === 'LOADING' || state.input === input) {
return
}
setState((state) => ({
...state,
fetchInfo: { state: 'LOADING' },
input,
}))
fetchStuff(input).then((result) => {
setState((state) => ({
...state,
value: result,
fetchInfo: { state: 'LOADED' },
}))
})
}, [input, state, setState])
return <div>{state.value}</div>
}
This code looks quite straightforward, still let me elaborate on it.
Read more: useEffectlessState for data fetching with React hooksIf you’re already using async/await syntax, you might notice that forEach is not working for asynced functions. In that case you might start doing old…
Recently I’ve got a pleasure to debug some bug inside node.js app. Run-time was breaking with the following error: Quite usual error, meaning that object…
Recently I’ve came across a couple of node.js projects which use NODE_ENV for defining environment for the Development, Testing, Acceptance and Production (DTAP) pipeline. At…
Today JSON is widely used in different corners of software development. It’s used as data format, as configuration or even as in memory database. At…
It’s going to be a short one, but powerful. Do you remember I wrote previously why getters/setters is a bad idea in JavaScript? I didn’t…
Do you know what would be the result of the following code executed in node.js without babel and any transpiling? example.js Do you know the…
When preparing application for deploying to production env I want to ensure that everything is properly logged, especially things which are unexpected in code. That’s…
If you’ve decided to move react components to es6/es2015 syntax you’ve might found out that defining propTypes and contextTypes is not that seamless as it…