State Management - defineState()
When developing a frontend, there are cases where you need to store user-entered strings or selected values as state and send them to the server as variables. Follow this guide to implement it effectively.
Declaring State: defineState()
To declare a state, use defineState()
. This function takes an object as an argument and sets it as the initial value.
Accessing State Value: .value
You can access the values of a declared state using the .value property, as shown below:
Tips
If specifying topPageState.
followed by the variable name declared with defineState
every time feels cumbersome, you can simplify this by destructuring the variables.
Updating State: .update()
To update a state value, use the update()
method.
Using Input Components
The Morph framework provides components to reflect user input into states declared with defineState(). For details, refer to Input Components.
By using input components, you can easily bind user input to the state.
Passing Values to Data Components
Data components can accept a variables property. By passing the values of states declared with defineState() to this property, you can send user-entered values to Python functions or SQL files.
By combining all the features we’ve covered so far, you can build a complete data app like this: