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

feat: v5 #152

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

feat: v5 #152

wants to merge 3 commits into from

Conversation

bluebill1049
Copy link
Member

@bluebill1049 bluebill1049 commented Dec 29, 2024

V5

Context

  • removed Context provider
  • introduce selector

CSB

https://codesandbox.io/p/sandbox/jgxq4q?file=%2Fsrc%2FApp.tsx%3A1%2C1-55%2C1

Example

import { createStore, useStateMachine } from "little-state-machine";

createStore({
  yourDetail: { firstName: "1", lastName: "2" }, // it's an object of your state
});

function updateYourDetail(state, payload) {
  return {
    ...state,
    yourDetail: {
      ...state.yourDetail,
      ...payload,
    },
  };
}

function Other() {
  const { actions, state, getState } = useStateMachine(
    {
      updateYourDetail,
    },
    (data) => data.yourDetail.firstName // only re-render when firstName changes
  );

  return <p>{state.yourDetail.firstName}</p>;
}

export default function App() {
  const { actions, state, getState } = useStateMachine({
    updateYourDetail,
  });

  return (
    <div className="App">
      <button
        onClick={() => {
          actions.updateYourDetail({
            firstName: Math.random().toString(),
            lastName: Math.random().toString(),
          });
        }}
      >
        actions
      </button>
      {state.yourDetail.firstName}
      {state.yourDetail.lastName}

      <Other />
    </div>
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant