Giter Site home page Giter Site logo

fazor's Introduction

fazor

description

a redux like state engine for react using 'useReducer' hook and constate

installation

npm i @fazor/fazor -S

usage

import React from 'react'
import ReactDOM from 'react-dom'

import { create as createFazor } from '@fazor/fazor'

// an initial state for your component
const myComponentInitialState = {
	i: 0,
	messages: [
		'fazors to stun',
		'faze transitions'
	]
}

const [ createAction, setInitialFaze ] = createFazor()

// 'createAction' accepts a type, an optional handler and an optional reducer
createAction({

	// the type is the name of the execute function returned by 'getActions'
	type: 'myAction',

	// the handler will be called when the action is executed
	//  -return an object from the handler to add properties to the action
	//  -return false to abort the dispatch
	handler: (i, messages) => ({ myMessage: messages[i] }),

	// the reducer will be passed the current state and the dispatched action
	//  -the new state must be returned
	reducer: ({ i, ...state }, { myMessage }) => ({
		...state,
		i: i === 0 ? 1 : 0,
		myMessage
	})
})

// 'setInitialFaze' accepts an object containing initial state
const getFaze = setInitialFaze({ ...myComponentInitialState })

// your container component that will dispatch 'myAction'
const MyComponent = ({ useFaze, createAction }) => {

	// 'getFaze' will give you a state and any actions you created
	const [ { i, messages }, { myAction } ] = getFaze()

	return (
		<button onClick={ () => myAction(i, messages) }>
			dispatch my action
		</button>
	)
}

const App = () => {
	// wrap your component in 'getFaze.Provider'
	return (
		<getFaze.Provider>
			<MyComponent />
		</getFaze.Provider>
	)
}

ReactDOM.render(
	<App />,
	document.getElementById('root')
)

fazor's People

Contributors

ironman9967 avatar

Watchers

James Cloos avatar  avatar

Forkers

yurikoex

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.