Member-only story
React best practices and patterns to reduce code — Part 3
5 min readMar 21, 2022
Press enter or click to view image in full size![]()
This is 3rd the article about React best practices, If you have not read my previous articles, please check them out.
React best practices and patterns to reduce code — Part 1
React best practices and patterns to reduce code — Part 2
let’s look at some more best practices and patterns to reduce code.
Store Tokens to an HTTP Cookie rather than localStorage
Bad code:
const token = localStorage.getItem("token");
if (token) {
axios.defaults.headers.common["Authorization"] = token;
}Good code:
import Cookies from "js-cookie"; // use another library if you wantconst token = Cookies.get("token");
if (token) {
axios.defaults.headers.common["Authorization"] = token;
}
Better code:
No Code 😉Note:
- Cookies are shared with all sites on the same…
