-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1807b1b
commit 5897a8d
Showing
9 changed files
with
340 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { useObserver, Observer } from 'rosma'; | ||
|
||
const observer = new Observer({ foo: 'bar', num1: 0, num2: 0 }); | ||
|
||
export function Example1() { | ||
const { foo, setFoo } = useObserver(observer); | ||
|
||
return ( | ||
<div> | ||
<p>Foo: {foo}</p> | ||
<button onClick={() => setFoo(foo === 'bar' ? 'baz' : 'bar')}> | ||
Toggle Foo | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
export function Example2() { | ||
const { foo, setFoo } = useObserver(observer); | ||
|
||
return ( | ||
<div> | ||
<p>Foo: {foo}</p> | ||
<button onClick={() => setFoo((foo) => (foo === 'bar' ? 'baz' : 'bar'))}> | ||
Toggle Foo | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
export function Example3() { | ||
const { firstName, lastName, set } = useObserver(observer); | ||
|
||
return ( | ||
<div> | ||
<p> | ||
Full Name: {firstName} {lastName} | ||
</p> | ||
<button onClick={() => set({ firstName: 'John', lastName: 'Doe' })}> | ||
Set full name | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
export function Example4() { | ||
const { num1, num2, set } = useObserver(observer); | ||
|
||
return ( | ||
<div> | ||
<p>num1:{num1}</p> | ||
<p>num2:{num2}</p> | ||
<button | ||
onClick={() => | ||
set(({ num1, num2 }) => ({ num1: num1 + 1, num2: num2 + 1 })) | ||
} | ||
> | ||
increment | ||
</button> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { | ||
Example1, | ||
Example2, | ||
Example3, | ||
Example4, | ||
} from '@/components/examples/use_observer'; | ||
|
||
export const meta = { | ||
keywords: 'useObserver.keywords', | ||
}; | ||
|
||
# useObserver | ||
|
||
useObserver.description | ||
|
||
```js | ||
import { useObserver } from 'rosma'; | ||
|
||
export default function Example() { | ||
const { foo, setFoo } = useObserver('bar'); | ||
|
||
return ( | ||
<div> | ||
<p>Foo: {foo}</p> | ||
<button onClick={() => setFoo(foo === 'bar' ? 'baz' : 'bar')}> | ||
Toggle Foo | ||
</button> | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
<Example1 /> | ||
|
||
useObserver.explain | ||
|
||
## Get the previous value from the setter | ||
|
||
useObserver.previousValue.description | ||
|
||
```js | ||
import { useObserver } from 'rosma'; | ||
|
||
export default function Example() { | ||
const { foo, setFoo } = useObserver('bar'); | ||
|
||
return ( | ||
<div> | ||
<p>Foo: {foo}</p> | ||
<button | ||
onClick={() => setFoo((prevFoo) => (prevFoo === 'bar' ? 'baz' : 'bar'))} | ||
> | ||
Toggle Foo | ||
</button> | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
<Example2 /> | ||
|
||
useObserver.previousValue.explain | ||
|
||
## Set multiple values at same time | ||
|
||
useObserver.multipleValues.description | ||
|
||
```js | ||
export function Example() { | ||
const { firstName, lastName, set } = useObserver(''); | ||
|
||
return ( | ||
<div> | ||
<p> | ||
Full Name: {firstName} {lastName} | ||
</p> | ||
<button onClick={() => set({ firstName: 'John', lastName: 'Doe' })}> | ||
Set full name | ||
</button> | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
<Example3 /> | ||
|
||
useObserver.multipleValues.explain | ||
|
||
## Passing function to `set` as parameter | ||
|
||
useObserver.setFunction.description | ||
|
||
```js | ||
export function Example4() { | ||
const { num1, num2, set } = useObserver(0); | ||
|
||
return ( | ||
<div> | ||
<p>num1: {num1}</p> | ||
<p>num2: {num2}</p> | ||
<button | ||
onClick={() => | ||
set(({ num1, num2 }) => ({ num1: num1 + 1, num2: num2 + 1 })) | ||
} | ||
> | ||
increment | ||
</button> | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
useObserver.setFunction.explain | ||
|
||
<Example4 /> | ||
|
||
**Keywords**: useObserver.keywords |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.