There is no single right answer for handling requests with React hooks. After a year of experience with hooks, I have come up with a pretty efficient pattern for that. You can adapt or modify the pattern for your own use.
I like to use Axios for making requests and I will make a useful hooks for handling cancellation tokens for it. You can modify this hooks to use fetch api with AbortController.
Create-React-App is maintained by the React’s creator, Facebook and it is one of the most popular boilerplate for React projects. As project grows, the compilation becomes slower and you will want to make a build faster. There are countless ways to improve this after ejecting from CRA, but today, I would like to introduce a way to make it faster without ejecting.
Using latest release of react-scripts
may help you boost the build speed.
How to update CRA to latest version.
swc is the blazing fast JavaScript/TypeScript transpiler written in Rust.
From the swc official page,
It’s 20x faster than…
When a React component is unmounted, belonging AJAX requests using Fetch/Axios no longer need to be executed. Sometimes, a not canceled request from an unmounted component may trigger some unexpected and unwanted updates.
Above code will try to make a request to url
and alert the stringified response data after 5 seconds.
It will actually never be able to make the request because controller.abort()
is executed before the inner function of the setTimeout
runs.
This code is doing identical task as the above example using Fetch
. The only difference is that while Fetch
is using AbortController
for…
In TypeScript 3.7, there are some interesting new features like optional chaning and nullish coalescing. When I tried to install and configure to try using new features, there were some syntax highlight breaking and my VS Code was not able to detect the new features correctly. Now I know how to resolve them and I would like to share them.
When I was writing this post, I used VS Code v1.40.1 and Node.js v12.13.0.
Go to your project root directory and run this.
npm install typescript@3.7.2
The latest TypeScript version is 3.7.2 now. If you want…
I will guide you to make a Node.js app that crawls tweets from Twitter and calculates a keyword’s sentiment analysis trend in last 24 hours. I will make the code to be generic to use any keywords but in the sample code, I will use “Bitcoin” as the keyword.
Beware that these settings may or may not work if you are using different versions of packages. I used Node.js v10.16.0 for running this.
consumer_key
, consumer_secret
, access_token
and access_token_secret
.You…
Setting up Node.js App with Nginx is ideal for running node app in production. As Express’ performance best practice performance states, letting Nginx to handle cached requests, compressions and serve static files is recommended. Node.js is well known to be good at heavy IOs rather than handling static file serving and CPU intensive compressions.
I assume that the static files are at /usr/src/app/public
and Express uses 3000
port.
You may have static files serving configured in Express. You don’t need that any more since Nginx will handle that.
Find a line looks like this and remove it.
app.use(express.static('public'))
Also you…
I will describe the way to configure TypeScript unit testing with test coverage. I used TypeScript, Mocha, Chai and Istanbul for setting those up. I will guide you by creating a simple codes, belonging test codes and configurations that work altogether.
Beware that these settings may or may not work if you are using different versions of packages. I used Node.js v10.16.0 for running this.
You need create a directory, mkdir sample && cd sample
and
run npm init -y
on it.
It will give you a package.json
file like this.
Now it is time to install the needed…
Software Engineer