Skip to content

Commit 512e68f

Browse files
Openblocks-docsgitbook-bot
Openblocks-docs
authored andcommitted
GitBook: [lowcoder-org#74] add docs: "Temporary state"
1 parent 15d0644 commit 512e68f

File tree

11 files changed

+109
-5
lines changed

11 files changed

+109
-5
lines changed
70.6 KB
Loading
23.3 KB
Loading
84.3 KB
Loading
15.5 KB
Loading
160 KB
Loading
175 KB
Loading
266 KB
Loading
219 KB
Loading

docs/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* [Event handlers](build-apps/event-handlers.md)
2525
* [Write JavaScript](build-apps/write-javascript/README.md)
2626
* [Use third-party libraries](build-apps/use-third-party-libraries.md)
27+
* [Temporary state](build-apps/write-javascript/temporary-state.md)
2728
* [Component guides](build-apps/component-guides/README.md)
2829
* [Option lists](build-apps/component-guides/option-lists.md)
2930
* [Version and release management](build-apps/version-and-release-management.md)
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Temporary state
2+
3+
You can use temporary state to store and reference local data within an app. Every time you load or refresh an app, the value of temporary state value is reset.
4+
5+
## Use case scenarios
6+
7+
Temporary states may help in the following scenarios:
8+
9+
* To track the temporary values of a variable when the user interacts with your app.
10+
* To store your data only in operation without persisting to a database.
11+
* To function as a temporary property when built-in properties in Openblocks (such as `{{table.selectedRow}}` and `{{select.value}}`) do not support your use case.
12+
13+
{% hint style="info" %}
14+
To store and access data across apps in your workspace, use localStorage instead.
15+
{% endhint %}
16+
17+
## Create a temporary state
18+
19+
Click **+ New** and select **Temporary state** in query editor.
20+
21+
<figure><img src="../../.gitbook/assets/temporary-state-1.png" alt=""><figcaption></figcaption></figure>
22+
23+
You can rename the temporary state and set an initial value.
24+
25+
<figure><img src="../../.gitbook/assets/temporary-state-2.png" alt=""><figcaption></figcaption></figure>
26+
27+
## Set state values
28+
29+
Temporary state offers `setValue()` and `setIn()` methods to set or change its value, which can be called in JavaScript queries.
30+
31+
Use `setValue()` to change the value directly.
32+
33+
```javascript
34+
//state.setValue(value: any)
35+
state.setValue(3)
36+
state.setValue(input1.value)
37+
```
38+
39+
When the initial value of a temporary state is an object, use `setIn()` to change the value in a specified path.
40+
41+
```javascript
42+
// initial value of state2 as follows:
43+
{
44+
girl: {
45+
name: "Lucy",
46+
age: 18,
47+
city: {
48+
name: "New York"
49+
}
50+
}
51+
boy: {
52+
name: "Bob",
53+
age: 21,
54+
city: {
55+
name: "Los Angeles"
56+
}
57+
}
58+
}
59+
//To change the value in a specified path
60+
//state.setIn(path, any value)
61+
//path: an array of keys or indexes. Only the last item in the path will be changed.
62+
state2.setIn(['girl','city'],{name:'Seatle'})
63+
state2.setIn(['boy','age'],18)
64+
65+
66+
// To set value array value, you can use
67+
// init value = ["hello", "world"]
68+
state2.setIn([1],"foo") // this will result to ["hello", "foo"]
69+
```
70+
71+
You can also call these two methods in [event handlers](../event-handlers.md). Select **Set temporary state** as the action and choose method on demand.
72+
73+
<figure><img src="../../.gitbook/assets/temporary-state-3.png" alt=""><figcaption></figcaption></figure>
74+
75+
## Example: Increment counter
76+
77+
In this example, the counter tracks the number of button clicks. Every time the user clicks the button, the number in the text component increases by one.
78+
79+
<figure><img src="../../.gitbook/assets/temporary-state-4.png" alt=""><figcaption></figcaption></figure>
80+
81+
Build an increment counter in following steps:
82+
83+
1. Add a button component `button1` and a text component `text1`.
84+
2. Create a temporary state `state1`, set its initial value as `0`. Bind `{{state1.value}}` as the display text of `text1`.
85+
86+
<figure><img src="../../.gitbook/assets/temporary-state-5.png" alt=""><figcaption></figcaption></figure>
87+
3. Add an event handler for `button1`. Select the action **Set temporary state** and the method **setValue**, and then set `{{state1.value+1}}` as the value.
88+
89+
<figure><img src="../../.gitbook/assets/temporary-state-6.png" alt=""><figcaption></figcaption></figure>
90+
4. Click the button, you can see the value of `text1` increases by one each time you click.
91+
92+
<figure><img src="../../.gitbook/assets/temporary-state-7.gif" alt=""><figcaption></figcaption></figure>
93+
94+
You can also achieve the same result using JavaScript queries:
95+
96+
1. Add a new query, select **Run JavaScript code**.
97+
2. Write JavaScript query with this code, and set it to be manually invoked:\
98+
`state1.setValue(state1.value + 1)`
99+
3. Add an event handler of `button1` to run `query1`.
100+
101+
<figure><img src="../../.gitbook/assets/temporary-state-8.png" alt=""><figcaption></figcaption></figure>
102+
103+
Now click the **Increment counter** button, you should see the same result as above.

docs/self-hosting/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Follow the steps below:
5050
Run the command below:
5151

5252
{% code overflow="wrap" %}
53-
```powershell
53+
```bash
5454
docker run -d --name openblocks -p 3000:3000 -v "$PWD/stacks:/openblocks-stacks" openblocksdev/openblocks-ce
5555
```
5656
{% endcode %}
@@ -71,7 +71,7 @@ Add an environment variable `LOCAL_USER_ID` in `docker-compose.yml` downloaded i
7171
Add an environment variable `LOCAL_USER_ID` to the deploying command, as shown below:
7272

7373
{% code overflow="wrap" %}
74-
```docker
74+
```bash
7575
docker run -d --name openblocks -e LOCAL_USER_ID = YOUR_USER_ID -p 3000:3000 -v "$PWD/stacks:/openblocks-stacks" openblocksdev/openblocks-ce
7676
```
7777
{% endcode %}
@@ -94,7 +94,7 @@ With an SSL certificate, you can securely visit self-hosted Openblocks with HTTP
9494
2. Change the `ports` of the deploying command to `3443:3443`, as shown below:
9595

9696
{% code overflow="wrap" %}
97-
```
97+
```bash
9898
docker run -d --name openblocks -p 3443:3443 -v "$PWD/stacks:/openblocks-stacks" openblocksdev/openblocks-ce
9999
```
100100
{% endcode %}
@@ -107,7 +107,7 @@ docker run -d --name openblocks -p 3443:3443 -v "$PWD/stacks:/openblocks-stacks"
107107
{% tab title="Docker-Compose" %}
108108
Run the following commands to update to the latest Openblocks image:
109109

110-
```powershell
110+
```bash
111111
docker-compose pull
112112
docker-compose rm -fsv openblocks
113113
docker-compose up -d
@@ -118,7 +118,7 @@ docker-compose up -d
118118
Run the following commands to update to the latest Openblocks image:
119119

120120
{% code overflow="wrap" %}
121-
```powershell
121+
```bash
122122
docker pull openblocksdev/openblocks-ce
123123
docker rm -fv openblocks
124124
docker run -d --name openblocks -p 3000:3000 -v "$PWD/stacks:/openblocks-stacks" openblocksdev/openblocks-ce

0 commit comments

Comments
 (0)