Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelgja authored Nov 22, 2024
1 parent efe5101 commit 42a2bd8
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ yarn add muya@latest
```typescript
import { create } from 'muya';

const counter = create(0);
const useCounter = create(0);

// Access in a React component
function Counter() {
const count = counter(); // Call state directly
const count = useCounter(); // Call state directly
return (
<div>
<button onClick={() => counter.set((prev) => prev + 1)}>Increment</button>
<button onClick={() => useCounter.set((prev) => prev + 1)}>Increment</button>
<p>Count: {count}</p>
</div>
);
Expand Down Expand Up @@ -76,9 +76,11 @@ const asyncCountSlice = state.select(async (s) => {

### **Combine Multiple States**

Combine multiple states into a derived state:
Combine multiple states into a derived state via `select` method:

```typescript
import { create, select } from 'muya'

const state1 = create(1);
const state2 = create(2);

Expand Down Expand Up @@ -110,7 +112,7 @@ const derived = select([state1, state2], (s1, s2) => s1 + s2, (prev, next) => pr
Access state directly or through `useValue` hook:

### **Option 1: Access State Directly**

Each state can be called as the hook directly
```typescript
const userState = create(0);

Expand All @@ -121,7 +123,7 @@ function App() {
```

### **Option 2: Use the Hook**

Or for convenience, there is `useValue` method
```typescript
import { useValue } from 'muya';

Expand All @@ -132,7 +134,7 @@ function App() {
```

### **Option 3: Slice with Hook**

For efficient re-renders, `useValue` provides a slicing method.
```typescript
function App() {
const count = useValue(state, (s) => s.count); // Use selector in hook
Expand Down

0 comments on commit 42a2bd8

Please sign in to comment.