Local / global states

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:


class MyComponent extends Component {
  constructor(props) {
    super(props);

    this.state = {
      count: 0,
    };
  }

  handleClick() {
    this.setState({
      count: this.state.count + 1,
    });
  }

  render() {
    return (
      <div>
        The count is: {this.state.count}
        <button onClick={this.handleClick}>Click me!</button>
      </div>
    );
  }
}

export default MyComponent;


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:


import React, { useState } from 'react';

const App = () => {
  const [location, setLocation] = useState({
    latitude: 0,
    longitude: 0,
  });

  const handleChange = (e) => {
    const { latitude, longitude } = e.target.value;
    setLocation({ latitude, longitude });
  };

  return (
    <div>
      <input
        type="text"
        placeholder="Enter your latitude"
        value={location.latitude}
        onChange={handleChange}
      />
      <input
        type="text"
        placeholder="Enter your longitude"
        value={location.longitude}
        onChange={handleChange}
      />
    </div>
  );
};

export default App;


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.

DOKS

This template is a great way to showcase your application's documentation in full with code and imagery.

DOKS

This template is a great way to showcase your application's documentation in full with code and imagery.

DOKS

This template is a great way to showcase your application's documentation in full with code and imagery.