Local / global states
DoksJS components have two main ways to store data: local state and global state.
Local state is a powerful way to store data in DoksJS components. It allows you to create components that are more dynamic and interactive. Global state is a way to share data between components in an application. This can be useful for storing data that is common to all components in the application, such as the user's current location or the current time.
To manage local state in a DoksJS component, you need to use the state
property of the component. The state
property is an object that stores the state of the component. You can use the setState()
method to update the state of the component.
To manage global state in a DoksJS application, you can use the useState()
hook. The useState()
hook returns an array with two values: the current value of the state and a function that can be used to update the state.
Here is an example of a DoksJS component that uses local state to track the number of times the button has been clicked:
This component has a state variable called count
. The count
variable tracks the number of times the button has been clicked. The handleClick()
method is called when the button is clicked and it increments the count
variable. The render()
method renders the component's HTML markup and sets the count
prop to the value of the count
variable.
Here is an example of a DoksJS application that uses global state to track the user's current location:
This application uses the useState()
hook to manage the global state of the user's current location. The handleChange()
method is called when the user changes the value of the latitude or longitude input fields. The handleChange()
method updates the global state of the user's current location.