Skip to content

Commit

Permalink
feat(website): improve search (pmndrs#2097)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandren authored Aug 28, 2023
1 parent 0fe69af commit 9001092
Show file tree
Hide file tree
Showing 43 changed files with 283 additions and 239 deletions.
30 changes: 15 additions & 15 deletions docs/basics/comparison.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ Jotai has two principles.
- Primitive: Its basic API is simple, like `useState`.
- Flexible: Atoms can derive another atom and form a graph. Atoms can also be updated by another arbitrary atom. It allows abstracting complex state models.

## How is Jotai different from useContext of React?
### How is Jotai different from useContext of React?

Jotai's core API is minimalistic and makes it easy to build utilities based upon it.

### Analogy
#### Analogy

You can view Jotai as a drop-in replacement to `useContext`. Except Jotai is aiming for simplicity, minimalistic API and can do much more than `useContext` & `useState`.

### Usage difference
#### Usage difference

We can see how we used to share data to children, compared to how we do it with Jotai. But let's use a real-world example where we have multiple `Context` in our app.

Expand Down Expand Up @@ -96,62 +96,62 @@ const Component2 = () => {
}
```

## How is Jotai different from Zustand?
### How is Jotai different from Zustand?

### Name
#### Name

Jotai means "state" in Japanese.
Zustand means "state" in German.

### Analogy
#### Analogy

Jotai is like Recoil.
Zustand is like Redux.

### Where state resides
#### Where state resides

To hold states,
Both have stores that can exist either at module level or at context level.
Jotai is designed to be context first, and module second.
Zustand is designed to be module first, and context second.

### How to structure state
#### How to structure state

Jotai state consists of atoms (i.e. bottom-up).
Zustand state is one object (i.e. top-down).

### Technical difference
#### Technical difference

The major difference is the state model. Zustand is a single store (although you could create multiple separate stores), while Jotai consists of primitive atoms and allows composing them together. In this sense, it's the matter of programming mental model.

### When to use which
#### When to use which

- If you need a replacement for useState+useContext, Jotai fits well.
- If you want a simple module state, Zustand fits well.
- If code splitting is important, Jotai should perform well.
- If you prefer Redux devtools, Zustand is good to go.
- If you want to make use of Suspense, Jotai is the one.

## How is Jotai different from Recoil?
### How is Jotai different from Recoil?

(Disclaimer: the author is not very familiar with Recoil, this may be biased and inaccurate.)

### Developer
#### Developer

- Jotai is developed with collective work by a few developers in Poimandres (formerly react-spring) org.
- Recoil is developed by a team at Facebook.

### Basis
#### Basis

- Jotai is focusing on primitive APIs for easy learning, and it's unopinionated. (The same philosophy with Zustand)
- Recoil is all-in-one, and it has various cache strategies.

### Technical difference
#### Technical difference

- Jotai depends on atom object referential identities.
- Recoil depends on atom string keys.

### When to use which
#### When to use which

- If you want to learn something new, either should work.
- If you like Zustand, Jotai would also be pleasant.
Expand Down
4 changes: 2 additions & 2 deletions docs/core/atom.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ derivedAtom.onMount = (setAtom) => {
}
```

## Advanced API
### Advanced API

Since Jotai v2, the `read` function has the second argument `options`.

### `options.signal`
#### `options.signal`

It uses [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) so that you can abort async functions.
Abort is triggered before new calculation (invoking `read` function) is started.
Expand Down
10 changes: 5 additions & 5 deletions docs/guides/atoms-in-atom.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ nav: 7.12
Atom configs don't have string keys and we identify them with referential equality.
In other words, we can use an atom config like a key.

## Storing an atom config in useState
### Storing an atom config in useState

First things first, we can store an atom config in useState.

Expand Down Expand Up @@ -44,7 +44,7 @@ const Component = () => {
}
```

## Storing an atom config in atom
### Storing an atom config in atom

Likewise, we can store an atom config as a value of another atom.

Expand Down Expand Up @@ -84,7 +84,7 @@ const derivedNameAtom = atom((get) => get(get(showingNameAtom)))
To avoid confusing what is in atoms, naming atoms explicitly would be important.
Also, TypeScript type information would be helpful.

## Storing an array of atom configs in atom
### Storing an array of atom configs in atom

Finally, the atoms in atom pattern is to store an array of atom config into an atom.

Expand Down Expand Up @@ -122,13 +122,13 @@ only the corresponding Counter component re-renders and no other components re-r

It is important to note that `anAtom.toString()` returns a unique id, which can be used as a `key` in a map.

### Hint for TypeScript users
#### Hint for TypeScript users

```jsx
<Counter countAtom={countAtom} key={`${countAtom}`} />
```

## Storing a map of atom configs in atom
### Storing a map of atom configs in atom

Likewise, we can store an object map instead of an array.

Expand Down
12 changes: 6 additions & 6 deletions docs/guides/composing-atoms.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ to implement a functionality.
Let's recap how we can derive an atom.

## Basic derived atoms
### Basic derived atoms

Here's one of the simplest examples of a derived atom:

Expand Down Expand Up @@ -40,7 +40,7 @@ So, we can only export `textUpperCaseAtom` and can hide

Now, let's see some real examples.

## Overriding default atom values
### Overriding default atom values

Suppose we have a read-only atom.
Obviously read-only atoms are not writable, but we can combine two atoms
Expand Down Expand Up @@ -69,7 +69,7 @@ in `jotai/utils`. See [atomWithDefault](../utilities/resettable.mdx).
Next, let's see another example to sync with external value.
## Syncing atom values with external values
### Syncing atom values with external values
There are some external values we want to deal with.
`localStorage` is the one. Another is `window.title`.
Expand Down Expand Up @@ -107,7 +107,7 @@ will be in sync.
Back to the main topic, let's explore another example.
## Extending atoms with `atomWith*` utils
### Extending atoms with `atomWith*` utils
We have several utils whose names start with `atomWith`.
They create an atom with a certain functionality.
Expand Down Expand Up @@ -137,7 +137,7 @@ It would still be a open research field.
Finally, let's see another example with actions.
## Action atoms
### Action atoms
This should be known pattern as it's described in README.
Nonetheless, it might to be useful to revisit.
Expand Down Expand Up @@ -193,7 +193,7 @@ export const dispatchAtom = atom(null, (_get, set, action) => {
Why do we want it? Because it will be used only when needed.
It allows code splitting and dead code elimination.
## In summary
### In summary
Atoms are building block.
By composing atoms based on other atoms,
Expand Down
4 changes: 2 additions & 2 deletions docs/guides/core-internals.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ nav: 7.10
This guide is beneficial for those who want to understand the core implementation of Jotai. It's not meant to be a complete example of the
core implementation but rather a simplified version. It's inspired by the collection of tweets by Daishi Kato([@dai_shi](https://twitter.com/dai_shi)).

## First Version
### First Version

Let's start with an easy example. An atom is just a function that will return a configuration object. We are using WeakMap to map atom with their state.
WeakMap doesn't keep its keys in memory, so if an atom is garbage collected, its state will be garbage collected too. This helps avoid memory leaks.
Expand Down Expand Up @@ -60,7 +60,7 @@ Here's an example using our simplified atom implementation. [Counter example](ht

Ref tweet: [Demystifying the internal of jotai](https://twitter.com/dai_shi/status/1484835169475653634)

## Second Version
### Second Version

Hang on! We can do better. In Jotai, we can create derived atom. A derived atom is an atom that depends on other atoms.

Expand Down
4 changes: 2 additions & 2 deletions docs/guides/gatsby.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ keywords: gatsby
status: draft
---

## Hydration
### Hydration

Jotai has support for hydration of atoms with `useHydrateAtoms`. The documentation for the hook can be seen [here](../utils/ssr.mdx).

## Babel plugins
### Babel plugins

Jotai provides Babel plugins for better DX while developing with Gatsby. [Find more info in the Babel section.](../tools/babel.mdx)
Loading

0 comments on commit 9001092

Please sign in to comment.