useStore - Atomic Data Docs

Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Atomic Data Docs

useStore

You can use useStore when you need direct access to the store in your components.

For example, on a login screen, you might want to set the agent on the store by calling store.setAgent after the user has entered their agent secret.

import { Agent, useStore } from '@tomic/react';

export const Login = () => {
  const store = useStore();
  const [agentSecret, setAgentSecret] = useState('');

  const login = () => {
    try {
      const newAgent = Agent.fromSecret(agentSecret);
      store.setAgent(newAgent);
    } catch(e) {
      console.error(e);
      // Show error.
    }
  };

  return (
    <label>
      Agent Secret
      <input
        type="password"
        placeholder="My agent secret"
        value={agentSecret}
        onChange={e => setAgentSecret(e.target.value)}
      />
    </label>
    <button onClick={login}>Login</button>
  );
};

Reference

Paramaters

None.

Returns

Store - The store object.