Skip to content

useStateStatus

This hook returns the state and its current status using a state handler.

Parameters

  1. the initial state value.
  2. the handler of the current state that takes the value as the parameter and return a status value.

Return Values

It returns a tuple of 4 values:

IndexNameTypeDescription
0valueAnythe current value.
1setValueFunctionupdates the value.
2statusAnythe status of the current value (what the handler returns).
4setStatusFunctionupdates the status value manually.

Example Usage

tsx
import { useStateStatus } from 'react-pre-hooks';

export default function Email() {
  const [email, setEmail, status] = useStateStatus('', value => {
    if (/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/.test(value)) return 'valid';
    else return 'invalid';
  });

  return (
    <main>
      <input type="text" value={email} onChange={e => setEmail(e.target.value)} />
      <p>this email is {status}.</p>
    </main>
  );
}
import { useStateStatus } from 'react-pre-hooks';

export default function Email() {
  const [email, setEmail, status] = useStateStatus('', value => {
    if (/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/.test(value)) return 'valid';
    else return 'invalid';
  });

  return (
    <main>
      <input type="text" value={email} onChange={e => setEmail(e.target.value)} />
      <p>this email is {status}.</p>
    </main>
  );
}

You can use objects instead:

tsx
import { useStateStatus } from 'react-pre-hooks';

export default function Username() {
  const [name, setName, status] = useStateStatus('', value => {
    if (value.length < 3) {
      return { type: 'length', message: 'too short!' };
    }
    if (value.length > 15) {
      return { type: 'length', message: 'too long!' };
    }
    if (/[^\w_]/.test(value)) {
      return { type: 'pattern', message: 'no special characters!' };
    }
  });

  return (
    <main>
      <input
        type="text"
        value={name}
        placeholder="Username"
        onChange={e => setName(e.target.value)}
      />
      {status && <p>{status.message}</p>}
    </main>
  );
}
import { useStateStatus } from 'react-pre-hooks';

export default function Username() {
  const [name, setName, status] = useStateStatus('', value => {
    if (value.length < 3) {
      return { type: 'length', message: 'too short!' };
    }
    if (value.length > 15) {
      return { type: 'length', message: 'too long!' };
    }
    if (/[^\w_]/.test(value)) {
      return { type: 'pattern', message: 'no special characters!' };
    }
  });

  return (
    <main>
      <input
        type="text"
        value={name}
        placeholder="Username"
        onChange={e => setName(e.target.value)}
      />
      {status && <p>{status.message}</p>}
    </main>
  );
}

Released under the MIT License.