React best practices and patterns to reduce code — Part 3

Rahul Sharma (DevsMitra)
5 min readMar 21, 2022
React best practices and patterns to reduce code | Rahul Sharma(DevsMitra)

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…

--

--

Rahul Sharma (DevsMitra)
Rahul Sharma (DevsMitra)

Written by Rahul Sharma (DevsMitra)

I’m a technology enthusiast who does web development. Passionate to contribute to open-source projects and make cool products.

No responses yet