8.2.2 • Published 7 months ago

common-react-toolkit v8.2.2

Weekly downloads
-
License
ISC
Repository
github
Last release
7 months ago
Toolkit of common React things to make developer's life a bit easier.

State Management

Thinking about state management in react & the sisters, it's generally hard to manage the state with different kinds of situations in mind. Some of them are as follows:

  • Is it feasible to use this state as global?
  • How do access the state in a clean & readable way?
  • What happens to the component tree when the parent's state changes?
  • What is the best way to update the state efficiently?
  • How can the files be structured to achieve the maximum understanding of the codebase & state at the same time?

Well, if you have ever experienced the react ecosystem, perhaps you already encountered at least one of the questions in the thought process of writing your application.

To get answers, it's suggested to use the state management functionality of the library. Following the philosophy below, the library is architectured to suit the needs of virtually every scenario out there.

Given the ability to break the application's state into smaller chunks called stores, components can access only the required ones to help them achieve max. efficiency & neatness with the use of react hooks.

Indeed, it's also hard to learn new libraries. Even harder, deciding whether to make it mainstream for my project or not?. Perhaps after reading the "easiest" usage pattern of the library, you will get convinced of the same.

Usage

Considering the fact that many developers hate the concept of the global state, the library is based on the concept of distributed global state which means despite being global every data variable has its own separate representation/access.

Creating store

import { makeStore } from "common-react-toolkit"

const [usernameStore, useUsername] = makeStore<string>("Titter")

You might understand the code by looking at it. But if you don't, here we go:

  • The first line imports the makeStore function from the library.
  • The second line, creates the store & hook respectively by calling the makeStore function which takes its "initial state" as the argument.

And, that's all there's is to it for creating a store!

Accessing store

Now if you want to use this store, just use it like a normal react hook. Consider the following component:

function SpookyComponent() {
	const username = useUsername()

	return <div>{username}</div>
}

And, that's how you access the value in the store.

You might be wondering, well there's no set function. You're right! Because there's no need for the same. Your component will get re-rendered as soon as the store's state changes.

Modifying store

Remember the usernameStore, provided by the makeStore? Yes, that's what enables you to handle the current state of the store or any component attached to the store.

Consider the following example:

import { makeStore } from "common-react-toolkit"

const [usernameStore, useUsername] = makeStore<string>("Titter")

function updateUsername(name: string) {
	usernameStore.set(name)
}

While updating javascript objects, you might have used { ...object } or object destructuring syntax in the past. Well, to avoid that you can use the .merge() function instead of .set() to achieve the same.

Found easy? Yep, it's as easy as it looks.

File structure

Although file structure is a subjective topic, the following one is preferred:

some-spooky-project-name/
├─ src/
│  ...
│  ├─ State/
│  │  ├─ User.ts
│  │  ├─ Auth.ts

How to use it at its best?

Have a look at the example folder in the repository.

8.2.2

7 months ago

7.4.4

9 months ago

7.4.3

9 months ago

7.4.2

9 months ago

7.4.1

9 months ago

7.4.8

9 months ago

7.4.7

9 months ago

7.4.6

9 months ago

7.4.5

9 months ago

8.1.0

8 months ago

8.1.1

7 months ago

7.4.0

9 months ago

8.2.1

7 months ago

8.2.0

7 months ago

8.0.1

9 months ago

8.0.0

9 months ago

8.0.3

9 months ago

8.0.2

9 months ago

7.3.2

11 months ago

7.3.1

11 months ago

7.3.0

11 months ago

7.1.0

12 months ago

7.2.0

11 months ago

6.6.5

1 year ago

6.6.7

12 months ago

6.6.6

1 year ago

6.6.9

12 months ago

6.6.8

12 months ago

7.0.0

12 months ago

6.6.4

1 year ago

6.6.1

1 year ago

6.6.0

1 year ago

6.6.3

1 year ago

6.6.2

1 year ago

6.5.4

1 year ago

6.5.3

1 year ago

6.5.6

1 year ago

6.5.5

1 year ago

6.5.8

1 year ago

6.5.7

1 year ago

6.5.0

1 year ago

6.5.2

1 year ago

6.5.1

1 year ago

6.4.2

1 year ago

6.4.0

1 year ago

6.3.5

1 year ago

6.3.4

2 years ago

6.3.3

2 years ago

6.1.0

2 years ago

6.3.0

2 years ago

6.3.2

2 years ago

6.3.1

2 years ago

6.0.0

2 years ago

6.2.0

2 years ago

5.3.0

2 years ago

5.1.0

2 years ago

4.2.2

2 years ago

4.0.0

2 years ago

4.2.1

2 years ago

4.2.0

2 years ago

5.2.4

2 years ago

5.2.2

2 years ago

5.2.1

2 years ago

5.2.0

2 years ago

5.0.0

2 years ago

3.3.2

2 years ago

4.1.0

2 years ago

3.3.1

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.2

2 years ago

2.3.0

2 years ago

2.2.1

2 years ago

2.1.2

2 years ago

2.2.0

2 years ago

2.1.1

2 years ago

2.1.4

2 years ago

2.4.0

2 years ago

2.1.3

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

3.1.3

2 years ago

3.3.0

2 years ago

3.1.2

2 years ago

3.0.3

2 years ago

3.2.0

2 years ago

3.1.1

2 years ago

3.0.2

2 years ago

3.1.0

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.3

2 years ago

0.1.0

2 years ago

0.0.8

2 years ago

0.0.5

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago