-
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
Showing
8 changed files
with
144 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
layout: default | ||
title: combineState | ||
nav_order: 6 | ||
parent: API | ||
--- | ||
|
||
## combineState | ||
|
||
> Note: this function needs to be imported from `"veles/utils"` | ||
> This function is also available as `combine` | ||
Sooner or later you'll run into a situation where you depend on several states. While in theory you can make a subscription inside another one and access both current values that way, you should not do that. Not only it is cumbersome to write, it is also not very flexible and can't be used with other states. | ||
|
||
To help with that, there is a `combineState` or `combine` function, which merges several states together for you. It accepts any number of states and creates a new state which will have an array with values from all passed states. | ||
|
||
```jsx | ||
import { createState } from "veles"; | ||
import { combineState } from "veles/utils"; | ||
|
||
function Component() { | ||
const nameState = createState(""); | ||
const lastNameState = createState(""); | ||
const fullNameState = combineState(nameState, lastNameState); | ||
|
||
return ( | ||
<div> | ||
<input | ||
type="text" | ||
name="name" | ||
onInput={(e) => nameState.setValue(e.target.value)} | ||
value={nameState.useAttribute()} | ||
/> | ||
<input | ||
type="text" | ||
name="lastName" | ||
onInput={(e) => lastNameState.setValue(e.target.value)} | ||
value={lastNameState.useAttribute()} | ||
/> | ||
{fullNameState.useValueSelector( | ||
([firstName, lastName]) => `Full name is ${firstName} ${lastName}` | ||
)} | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
The advantage here is that we can combine several states, we can pass that state down as a prop to another components, we can use `trackValue` and do side-effects when either of them change. |
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,30 @@ | ||
--- | ||
layout: default | ||
title: createElement | ||
nav_order: 5 | ||
parent: API | ||
--- | ||
|
||
## createRef | ||
|
||
Since components don't re-render, and the component's code is executed only one time when the component is being mounted, you probably don't need refs for anything except for more convenient access to DOM elements. Every element accepts `ref` and it will be assigned the correct HTML node at the time of execution. If you want to pass the ref down, there is no need for wrapping components into anything, just pass it as any other prop (`ref` on components is ignored and treated like any other property). | ||
|
||
```jsx | ||
import { createRef } from "veles"; | ||
function App() { | ||
const inputRef = createRef(); | ||
|
||
return ( | ||
<div> | ||
<button | ||
onClick={() => { | ||
inputRef.current?.focus(); | ||
}} | ||
> | ||
focus input | ||
</button> | ||
<input type="text" ref={inputRef} /> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
layout: default | ||
title: selectState | ||
nav_order: 7 | ||
parent: API | ||
--- | ||
|
||
## selectState | ||
|
||
> Note: this function needs to be imported from `"veles/utils"` | ||
> This function is also available as `select` | ||
When working with multiple states, often by using [combineState](./combine-state.html), you end up with not the ideal state value representation. E.g. if you want to pass it down as a property to multiple component, you might want to avoid doing the same selector work on all of them, and want to do them at once. It becomes even more important if you want to combine that state with something else. | ||
|
||
To change the state value, you can use `selectState`/`select` functions. Here is an example: | ||
|
||
```jsx | ||
import { createState } from "veles"; | ||
import { combineState, selectState } from "veles/utils"; | ||
|
||
function Component() { | ||
const nameState = createState(""); | ||
const lastNameState = createState(""); | ||
const fullNameState = selectState( | ||
combineState(nameState, lastNameState), | ||
([firstName, lastName]) => `${firstName} ${lastName}` | ||
); | ||
|
||
return ( | ||
<div> | ||
<input | ||
type="text" | ||
name="name" | ||
onInput={(e) => nameState.setValue(e.target.value)} | ||
value={nameState.useAttribute()} | ||
/> | ||
<input | ||
type="text" | ||
name="lastName" | ||
onInput={(e) => lastNameState.setValue(e.target.value)} | ||
value={lastNameState.useAttribute()} | ||
/> | ||
{fullNameState.useValue((fullName) => `Full name is ${fullName}`)} | ||
</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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.