Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
NotificationsYou must be signed in to change notification settings

pothos-dev/flux-registry

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Yet another Flux implementation for React, focusing on minimal boilerplate and strong Typescript support.

High level overview:

  • create Registry
    • declareState interface
  • register Actions
    • declarePayload interface
    • declare functionreduce: (State, Payload) => State
  • register Components
    • declareOwnProps
    • optionally declareSelectedProps and functionselectProps: (State, OwnProps) => SelectedProps
    • declare functionrender: (OwnProps & SelectedProps & Dispatch) => React.Element | null
  • create Store
    • provide initial state
  • put Store Provider into the Component Tree as parent of registered Components
  • dispatch registered Actions from Component'srender function to update state

Example usage:

import{createFluxRegistry}from'flux-registry'importReact= require('react')importReactDOM= require('react-dom')// first create a registryconstflux=createFluxRegistry<{// definition of global statemyNumber:number}>()// register any number of actionsconstincrementNumber=flux.registerAction<{// definition of action payloadamount:number}>({// unique type name for the actiontype:'increment',// function to update the state when action is dispatchedreduce:({myNumber},{amount})=>({myNumber:myNumber+amount})})// register any number of react componentsconstIncrementButton=flux.registerComponent<{// definition of component props (OwnProps)incrementBy:number}>({// render function. dispatch is provided with the props// use it to dispatch previously registered actions from event handlersrender:({incrementBy, dispatch})=>{constonClick=()=>dispatch(incrementNumber({amount:incrementBy}))return<buttononClick={ onClick}>Incrementby{ incrementBy}</button>}})// registered components can select additional props (SelectedProps) from the global stateconstNumberDisplay=flux.registerComponent<{// OwnPropsmultiplicationFactor:number},{// SelectedPropstotalValue:number}>({// must provide a selectProps function in this case: (State, OwnProps) => SelectedPropsselectProps:({myNumber},{multiplicationFactor})=>({totalValue:myNumber*multiplicationFactor}),// OwnProps and SelectedProps are merged into a single object for the render functionrender:({totalValue, multiplicationFactor})=>{consttext=`${totalValue} (multiplied by${multiplicationFactor})`return<p>{ text}</p>}})// create a store that holds a single instance of the global stateconststore=flux.createStore({initialState:{myNumber:0}})// Wrap all registered components into a single store.Provider at the root of your component tree.constApp=()=>(<store.Provider><NumberDisplaymultiplicationFactor={5}/><IncrementButtonincrementBy={2}/></store.Provider>)// That's it, we can render our appReactDOM.render(<App/>,document.getElementById('root'))

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp