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

effector-factorio

v1.3.0

Published

The simplest way to write re-usable features with React + Effector

Downloads

1,009

Readme

Effector Factorio

The simplest way to write re-usable features with React + Effector

Install

npm install effector-factorio

Why this?

People became to obsessed with using React Hooks, which results in components being littered with a lot of business logic.
And because of React runtime nature, writing logic inside components always leads to:

  • Unreadable code. Tons of useMemo, useCallback and hooks limitations make code way harder to read and support.
  • Low performance. A lot of memoization which anyway leads to extra re-renders.
  • Problems with testing. You could just write your logic, create its instance and just test it. Instead you have to render your component, click buttons and do other irrelevant stuff just to test your logic.
  • Extra responsibility. This one speaks for itself, components fastly get a lot of extra responsibility and break clean architecture.
  • Structural incongrity. View composition can be structurally different to logic composition. Also, most of the logic gets used in several places. Also, with hooks, when the layout changes, you rewrite your logic as well.

However, when you extract logic from components, it usually means that it will be a singleton. So, if you want to re-use it, you can't.

This library allows you to extract all the logic from components, while still having opportunity to re-use components.
It's based on factories and provides an API to make it in a unified way with less boilerplate.

Usage

This library consists of just two functions: modelFactory and modelView. Let's make a simple sign up form.

Step 1. Create model factory.

import { modelFactory } from 'effector-factorio';
import { combine, sample, createStore, createEvent, Effect } from 'effector';

type FactoryOptions = {
  registerFx: Effect<{ name: string; password: string }, any>;
};

export const factory = modelFactory((options: FactoryOptions) => {
  const loginChanged = createEvent<string>();
  const passwordChanged = createEvent<string>();
  const submitPressed = createEvent();

  const $login = createStore('');
  const $password = createStore('');

  const $form = combine({ login: $login, password: $password });

  const $disabled = options.registerFx.pending;

  $login.on(loginChanged, (prev, next) => next);
  $password.on(passwordChanged, (prev, next) => next);

  sample({
    source: $form,
    clock: submitPressed,
    target: options.registerFx,
  });

  return {
    $login,
    $password,
    $disabled,
    loginChanged,
    passwordChanged,
    submitPressed,
  };
});

Here we created a factory that creates and returns model instance.
And, as an example of customization, we can also pass external registerFx effect for each instance.

Step 2. Create a view.

import { useStore } from 'effector-react'
import { modelView } from 'effector-factorio'

import { factory } from './factory' // from step 1

const Form = modelView(factory, () => {
  return (
    <div className="flex flex-col gap-2">
      <LoginField />
      <PasswordField />
      <RegisterButton />
    </div>
})

const LoginField = () => {
  const model = factory.useModel()
  const login = useStore(model.$login)

  return <input
    value={login}
    placeholder="Login"
    onChange={evt => model.loginChanged(evt.target.value)}
  />
}

const PasswordField = () => {
  const model = factory.useModel()
  const password = useStore(model.$password)

  return <input
    value={password}
    placeholder="Password"
    onChange={evt => model.passwordChanged(evt.target.value)}
  />
}

const RegisterButton = () => {
  const model = factory.useModel()
  const disabled = useStore(model.$disabled)

  return (
    <button
      disabled={disabled}
      onClick={() => model.submitPressed}
    >
      Save
    </button>
  )
}

Here, modelView wraps component into HOC that accepts model prop with the current model instance and passes it through React Context.

Step 3. Export the whole thing

export const CreateUser = {
  factory,
  Form,
};

Step 4. Use it wherever you want

import { CreateUser } from '@/features/create-user';

const createUserModel = CreateUser.factory.createModel({
  registerFx: registerUserFx,
});

const Page = () => {
  return <CreateUser.Form model={createUserModel} />;
};

And here, we created model instance and passed it as a prop to our Form component

That's it! The benefit might be not that obvious on simple example, but I decided to keep it small in order to avoid frustration from huge irrelevant code. The key point is that if you correctly split your app into multiple layers, each segment will look small and clean, and you can easily compose all the stuff.

Utilities

Model<Factory> - utility type to extract model type from factory

Usage:

import { modelFactory, Model } from 'effector-factorio';

const fooFactory = modelFactory(() => {
  return {
    foo: createStore(''),
  };
});

type FooModel = Model<typeof fooFactory>;
// { foo: Store<string> }