juststore 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/form.js +31 -19
  2. package/package.json +1 -1
package/dist/form.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
  'use client';
3
3
  import { pascalCase } from 'change-case';
4
- import { useId } from 'react';
4
+ import { useEffect, useId, useMemo } from 'react';
5
5
  import { getSnapshot, produce } from './impl';
6
6
  import { createNode } from './node';
7
7
  import { createStoreRoot } from './root';
@@ -36,7 +36,7 @@ function useForm(defaultValue, fieldConfigs = {}) {
36
36
  const errorNamespace = `errors.${namespace}`;
37
37
  const storeApi = createStoreRoot(namespace, defaultValue, { memoryOnly: true });
38
38
  const errorStore = createStoreRoot(errorNamespace, {}, { memoryOnly: true });
39
- const formStore = {
39
+ const formStore = useMemo(() => ({
40
40
  clearErrors: () => produce(errorNamespace, undefined, false, true),
41
41
  handleSubmit: (onSubmit) => (e) => {
42
42
  e.preventDefault();
@@ -45,8 +45,8 @@ function useForm(defaultValue, fieldConfigs = {}) {
45
45
  onSubmit(getSnapshot(namespace));
46
46
  }
47
47
  }
48
- };
49
- const store = new Proxy(storeApi, {
48
+ }), [namespace, errorNamespace]);
49
+ const store = useMemo(() => new Proxy(storeApi, {
50
50
  get(_target, prop) {
51
51
  if (prop in formStore) {
52
52
  return formStore[prop];
@@ -59,22 +59,34 @@ function useForm(defaultValue, fieldConfigs = {}) {
59
59
  }
60
60
  return undefined;
61
61
  }
62
- });
63
- for (const entry of Object.entries(fieldConfigs)) {
64
- const [path, config] = entry;
65
- const validator = getValidator(path, config?.validate);
66
- if (validator) {
67
- storeApi.subscribe(path, (value) => {
68
- const error = validator(value, store);
69
- if (!error) {
70
- errorStore.reset(path);
71
- }
72
- else {
73
- errorStore.set(path, error);
74
- }
75
- });
62
+ }), [storeApi, formStore, errorStore]);
63
+ const unsubscribeFns = useMemo(() => {
64
+ const unsubscribeFns = [];
65
+ for (const entry of Object.entries(fieldConfigs)) {
66
+ const [path, config] = entry;
67
+ const validator = getValidator(path, config?.validate);
68
+ if (validator) {
69
+ const unsubscribe = storeApi.subscribe(path, (value) => {
70
+ const error = validator(value, store);
71
+ if (!error) {
72
+ errorStore.reset(path);
73
+ }
74
+ else {
75
+ errorStore.set(path, error);
76
+ }
77
+ });
78
+ unsubscribeFns.push(unsubscribe);
79
+ }
76
80
  }
77
- }
81
+ return unsubscribeFns;
82
+ }, [fieldConfigs, storeApi, errorStore, store]);
83
+ useEffect(() => {
84
+ return () => {
85
+ for (const unsubscribe of unsubscribeFns) {
86
+ unsubscribe();
87
+ }
88
+ };
89
+ }, [unsubscribeFns]);
78
90
  return store;
79
91
  }
80
92
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "juststore",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "A small, expressive, and type-safe state management library for React.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",