npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

effie

v0.0.4

Published

State Management via React hooks

Downloads

2

Readme

Effie

Welcome to Effie, the friendly and powerful state management library for React applications! With Effie, you can harness the full potential of Functional components and React Hooks to effortlessly manage your application state.

What problems does Effie solve?

We all love React Hooks, don't we? The more we learn about them, the more we want to refactor our code and eliminate other cumbersome approaches like singleton fetch API instances, factory methods, and dependency injection techniques.

However, hooks do come with their limitations.

Firstly, hooks cannot be used outside of the React context. This means you can't use hooks, for example, in Redux's reducers.

Secondly, the order and count of hooks within a single component must remain the same across re-renders. This can be a challenge when you want to create an array of hooks and load multiple resources simultaneously, using for instance TanStack's useQuery. To work around this limitation, you may resort to a hacky approach of creating a list of invisible components, each invoking useQuery and pushing the query result to a global application state (such as the Redux store) or updating React Context.

Lastly, it's common to need data retrieved from a backend API in multiple places. The naive approach would be to use the useEffect hook every time you need to fetch the data. However, this leads to multiple requests for the same data from multiple components. To share data between components, you can use React Contexts or libraries like React Query, SWR, or Apollo Client. These libraries maintain internal caches and minimize duplicate requests. But personally, I believe in keeping the application state and mutation logic separate from the presentation layer.

With Effie, you can overcome these challenges and enjoy a clean and efficient state management solution. Let's dive in and unleash the power of Effie in your projects!

Create a store

To get started with Effie, you'll need to create a store for your application state. This process is similar to Redux:

import { createStore, useSelector } from "effie";
import { RootStore } from "./RootStore";

export const store = createStore(RootStore);
export type RootState = ReturnType<typeof store.getState>;
export const useAppSelector = useSelector.withTypes<RootState>();
export const StoreProvider = store.Provider;

Here, we import the necessary functions from "effie" and define our store using the createStore function. We also define the RootState type, which represents the type of our store's state. Additionally, we create a custom hook useAppSelector using useSelector.withTypes<RootState>() to access the state within our components.

Now, let's take a closer look at the RootStore.

import { from, state } from "effie";

export function RootStore() {
  const [ids, setIds] = useState([1, 2, 3]);

  return state({
    state1: from(State1),
    state2: from(State2),
    arrayOfStates: ids.map((id) => from(StateWithId, { id })),
    setIds,
  });
}

In the RootStore function, we define our application's state. It resembles a React Functional Component and returns a state object that consists of several sub-components. Here, we use the useState hook to manage the ids state. The arrayOfStates field is dynamically created based on the length of the ids state, using the map function to generate sub-states of type StateWithId. The code of State1, State2 and StateWithId is not listed here, but you can imagine they are similar to the RootStore - they can use hooks and return some properties and mutation functions.

Now, let's see how you can use the store in your code:

store.getState().setIds([4, 5, 6]);

Using store from React Components

First, wrap your application with Effie store provider:

<StoreProvider>
  <App />
</StoreProvider>

In a React component, you can use the useAppSelector hook to access the state and actions:

function MyComponent() {
  const { setIds, arrayOfStates } = useAppSelector((state) => state);

  return (
    <>
      <div>
        { arrayOfStates.map((state) => <StateComponent key={state.id} state={state} />) }
      </div>
      <button onClick={() => setIds([4, 5, 6])}>Set IDs</button>
    </>;
  );
}

In the above example, we use the useAppSelector hook to extract the setIds and arrayOfStates from the store's state. We can then use them in our component to render the StateComponent for each state and update the ids state by clicking the button.

With Effie, managing your application state becomes a breeze, allowing you to focus on building amazing user experiences. Let's continue exploring the powerful features of Effie!