Djinn-state react
Djinn-state helpers for react
Install
npm npm i --save djinn-state djinn-state-react
yarn yarn add djinn-state djinn-state-react
Examples
Using
import { Djinn, DjinnService } from 'djinn-state';
import { createDjinnConnector } from 'djinn-state-react';
export const djinn = new Djinn();
export const connect = createDjinnConnector(djinn);
export class CounterService extends DjinnService {
state = {
count: 0,
};
decrement = () => {
const count = this.state.count - 1;
this.patch({ count });
};
increment = () => {
const count = this.state.count + 1;
this.patch({ count });
};
}
import { djinn } from './djinn';
djinn.register(CounterService);
djinn.start();
import React from 'react';
import { connect } from './djinn';
import { CounterService } from './CounterService';
const HomePage = ({ services, state }) => (
<div>
<button onClick={services.CounterService.decrement}>-</button>
{state.CounterService.count}
<button onClick={services.CounterService.increment}>+</button>
</div>
);
export default connect({
CounterService,
})(HomePage);