Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@effector/contract

Package Overview
Dependencies
Maintainers
4
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@effector/contract

Connect effector models via equal contract

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
4
Weekly downloads
 
Created
Source

@effector/contract

Usage

import { contract } from '@effector/contract';

Create a model in a model.ts file and export all effector units:

import { createEvent, createStore } from 'effector';

export const buttonClicked = createEvent();
export const $counter = createStore(0);

$counter.on(buttonClicked, (counter) => counter + 1);

Create a page component in a page.tsx:

import React from 'react';
import { Text, Button } from 'my-ui';

export const ExamplePage = () => (
  <div>
    <Text>Counter: 1</Text>
    <Button onClick={() => {
    }}>Increment</Button>
  </div>
);

Now create effector units to correctly connects with:

import React from 'react';
import { createEvent, createStore } from 'effector';
import { useStore, useEvent } from 'effector-react';
import { Text, Button } from 'my-ui';

export const buttonClicked = createEvent();
export const $counter = createStore(0);

export const ExamplePage = () => (
  <div>
    <CounterText />
    <Increment />
  </div>
);

const CounterText = () => {
  const counter = useStore($counter);
  return <Text>Counter: {counter}</Text>;
};

const Increment = () => {
  const handler = useEvent(buttonClicked);
  return <Button onClick={handler}>Increment</Button>;
};

Also, you can use @effector/reflect:

import React from 'react';
import { createEvent, createStore } from 'effector';
import { reflect } from '@effector/reflect';
import { Text, Button } from 'my-ui';

export const buttonClicked = createEvent();
export const $counter = createStore(0);

export const ExamplePage = () => (
  <div>
    <CounterText />
    <Increment />
  </div>
);

const CounterText = reflect({
  view: Text,
  bind: { children: $counter.map((counter) => `Counter: ${counter}`) },
});

const Increment = reflect({
  view: Button,
  bind: {
    onClick: buttonClicked.prepend(() => {
    })
  },
});

Now you can connect the model with the page, create index.ts file:

import { contract } from '@effector/contract';
import * as model from './model';
import * as page from './page';

export { ExamplePage } from './page';

contract({ page, model });

Keywords

FAQs

Package last updated on 04 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc