Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setStore proxy #79

Merged
merged 10 commits into from
Jan 12, 2022
Merged

Add setStore proxy #79

merged 10 commits into from
Jan 12, 2022

Conversation

aralroca
Copy link
Collaborator

@aralroca aralroca commented Jan 5, 2022

Fixes #77
CC: @yathink3 @danielart

TODO:

  • Update docs
  • Add TypeScript types of setStore
  • Reduce Teaful to less than 1kb

@aralroca aralroca self-assigned this Jan 5, 2022
@github-actions
Copy link

github-actions bot commented Jan 5, 2022

Size Change: +74 B (+2%)

Total Size: 3.56 kB

Filename Size Change
dist/index.js 842 B +16 B (+2%)
dist/index.m.js 857 B +19 B (+2%)
dist/index.modern.js 945 B +16 B (+2%)
dist/index.umd.js 913 B +23 B (+3%)

compressed-size-action

@aralroca
Copy link
Collaborator Author

aralroca commented Jan 12, 2022

Example of usage:

const initialStore = { count: 0, name: 'Aral' }
const { setStore } = createStore(initialStore);

const resetStore = () => setStore(initialStore);
const resetCount = () => setStore.count(initialStore.count);
const resetName = () => setStore.name(initialStore.name);

// Component without any re-render (without useStore hook)
function Resets() {
  return (
    <>
      <button onClick={resetStore}>
        Reset store
      </button>
      <button onClick={resetCount}>
        Reset count
      </button>
      <button onClick={resetName}>
        Reset name
      </button>
    </>
  );
}

Another example:

const { useStore, setStore } = createStore({
  firstName: '',
  lastName: '' 
});

function ExampleOfForm() {
  const [formFields] = useStore()

  return Object.entries(formFields).map(([key, value]) => (
    <input 
      defaultValue={value} 
      type="text"
      key={key}
      onChange={e => {
        // Update depending the key attribute
        setStore[key](e.target.value)
      }} 
    />
  ))
}

@aralroca aralroca added this to the Before 1.0.0 milestone Jan 12, 2022
@aralroca aralroca added this to To do in 1.0 via automation Jan 12, 2022
1.0 automation moved this from To do to Progress Jan 12, 2022
@aralroca aralroca merged commit 97d343c into master Jan 12, 2022
1.0 automation moved this from Progress to Done Jan 12, 2022
@aralroca aralroca deleted the aral-add-setStore-proxy branch January 12, 2022 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
1.0
Done
Development

Successfully merging this pull request may close these issues.

Proposal: add setStore proxy
2 participants