Skip to content

reset

Method to reset the state back to its initial value.

Description

The reset method restores the state to the initial value that was provided when the Signify instance was created. This is useful for clearing forms, resetting counters, or returning to a default state.

Input/Output

InputTypeDescription
None-No parameters required
OutputTypeDescription
ReturnsvoidNo return value

Example

typescript
import { signify } from 'react-signify';
import React from 'react';

const sCounter = signify(0);
const sForm = signify({
  name: '',
  email: '',
  message: ''
});

function CounterComponent() {
  const count = sCounter.use();
  
  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => sCounter.set(count + 1)}>
        Increment
      </button>
      <button onClick={() => sCounter.reset()}>
        Reset to 0
      </button>
    </div>
  );
}

function FormComponent() {
  const formData = sForm.use();
  
  const handleReset = () => {
    sForm.reset(); // Resets to initial empty form
  };
  
  return (
    <div>
      <input 
        value={formData.name}
        onChange={(e) => sForm.set(({value}) => {
          value.name = e.target.value;
        })}
        placeholder="Name"
      />
      <button onClick={handleReset}>Reset Form</button>
    </div>
  );
}

Released under the MIT License.