All Projects → cult-of-coders → react-molecule

cult-of-coders / react-molecule

Licence: MIT license
Molecule is a light-weight framework that lets you reason about inter-component communication, dependency injection and logic decoupling.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to react-molecule

Tinystate
A tiny, yet powerful state management library for Angular
Stars: ✭ 207 (+491.43%)
Mutual labels:  flux
hypo-container
A dependency injection container.
Stars: ✭ 16 (-54.29%)
Mutual labels:  dependency-injection
realt
Realt is a new way to work with Redux inspired by Alt
Stars: ✭ 41 (+17.14%)
Mutual labels:  flux
Blazor Fluxor
DEPRECATED PROJECT. See FLUXOR
Stars: ✭ 216 (+517.14%)
Mutual labels:  flux
nanoflux-fusion
Redux-like extension for Nanoflux
Stars: ✭ 15 (-57.14%)
Mutual labels:  flux
StartupModules
Startup modules for ASP.NET Core.
Stars: ✭ 33 (-5.71%)
Mutual labels:  dependency-injection
Deox
Functional Type-safe Flux Standard Utilities
Stars: ✭ 200 (+471.43%)
Mutual labels:  flux
WebApiClient.Extensions
WebApiClient项目的第三方扩展:Autofac、DependencyInjection、HttpClientFactory、SteeltoeOSS.Discovery、MessagePack、Protobuf、Json-Rpc
Stars: ✭ 73 (+108.57%)
Mutual labels:  dependency-injection
DI-compiler
A Custom Transformer for Typescript that enables compile-time Dependency Injection
Stars: ✭ 62 (+77.14%)
Mutual labels:  dependency-injection
Gakko
Gakko - The Classroom App
Stars: ✭ 14 (-60%)
Mutual labels:  dependency-injection
Denormalizr
Denormalize data normalized with normalizr
Stars: ✭ 231 (+560%)
Mutual labels:  flux
react-store
Redux like store with React hooks and Context API
Stars: ✭ 16 (-54.29%)
Mutual labels:  flux
stashbox
A lightweight, fast, and portable dependency injection framework for .NET-based solutions.
Stars: ✭ 120 (+242.86%)
Mutual labels:  dependency-injection
Smitty
Tiny flux implementation built on mitt
Stars: ✭ 210 (+500%)
Mutual labels:  flux
nifi-influxdb-bundle
InfluxDB Processors For Apache NiFi
Stars: ✭ 30 (-14.29%)
Mutual labels:  flux
Flooks
🍸 A state manager for React Hooks
Stars: ✭ 201 (+474.29%)
Mutual labels:  flux
alice
An additive dependency injection container for Golang.
Stars: ✭ 51 (+45.71%)
Mutual labels:  dependency-injection
sector
Simple Injector; Dependency Injection
Stars: ✭ 12 (-65.71%)
Mutual labels:  dependency-injection
Autofac.Extras.NLog
An Autofac module to integrate Autofac and NLog, it supports both constructor and property injection.
Stars: ✭ 48 (+37.14%)
Mutual labels:  dependency-injection
gocontainer
Simple Dependency Injection Container
Stars: ✭ 18 (-48.57%)
Mutual labels:  dependency-injection

React Molecule

Build Status Coverage Status code style: prettier

Molecule has been built to allow creation of smart, hackable react libraries. Molecule is esentially a smart context object that allows you to do the following:

  • Handles listening, and emissions of events
  • Can encapsulate logic to allow easy testing and dependency injection
  • Enables component overriding via registry
  • Ability to manage a reactive store, isolated from your components

An example where react-molecule has been efficiently used is here: https://www.npmjs.com/package/easify

Install

npm install --save react-molecule

import { molecule, useMolecule } from "react-molecule";
const Page = molecule()(PageComponent);

const PageComponent = () => {
  const molecule = useMolecule();
  // Use it
};

Example

Molecule's flexibility is extreme. There are lots of way you can use it. Below we explore an example, where we have a list, and we want to refresh the list when clicking a button.

import { Agent } from "react-molecule";

// You define logic in Agents
class InvoiceLoader extends Agent {
  // This runs when the molecule is firstly initialised
  init() {
    this.loadInvoices();
  }

  loadInvoices() {
    const { store } = this.molecule;
    loadInvoiceQuery().then(result => {
      store.invoices = result;
    });
  }
}
import { molecule, useStore, useAgent } from "react-molecule";
import { observable } from "mobx";
import { observer } from "mobx-react";

// This initialises the molecule by injecting agents, and a reactive store
const InvoiceListPage = molecule(props => {
  return {
    agents: {
      // We want to have a single instance of Agent that can be configured
      invoiceLoader: InvoiceLoader.factory()
    },
    store: observable({
      invoices: []
    })
  };
})(InvoiceList);

const InvoiceList = observer(() => {
  // We can access the molecule's store directly
  const { invoices } = useStore();

  // We can also get access to the agents
  const invoiceLoader = useAgent("invoiceLoader");
  return (
    <ul>
      <li>
        <button onClick={() => invoiceLoader.loadInvoices()}>Refresh</button>
      </li>
      {invoices.map(invoice => {
        <InvoiceItem invoice={invoice} key={invoice._id} />;
      })}
    </ul>
  );
});

What do we gain exactly using this approach?

  • By isolating logic inside agents, testing React components logic transforms into testing Agents
  • We have a way to store reactive data, in which multiple agents can work together

This is just scratching the surface, let's explore more in the documentation.

Documentation

Start reading the documentation then use the API for reference.

API

After you read the documentation you can use the API for reference: Click here to read it

Support

Feel free to contact us at [email protected]

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].