React is an excellent library for creating interactive and stateful views. However, things become unclear when you need to share & change data across components.
Alfa is an intuitive and straightforward way to manage React state. It completely decouples the complex relationships between components and let you focus on making components that work anywhere.
Its simple design allows you to adopt it in a matter of minutes while at the same time provides your essential tools to keep your application code easy to change and understand. Here is a list of things why it is the perfect fit for your next React app:
Easy - Only 4 functions/APIs to learn.
Fast - Alfa wraps your components with a thin layer. It introduces a little or no performance impacts.
Small - ~ 190LOC & 3KB minified + gzipped.
Async - Alfa supports asynchronous operation natively without additional packages.
Explicit - Alfa lets you know what a component requires (input) and what it changes (output).
Transparent - You can use and unit test your components as it is without Alfa. Users of your lib/component could but don't have to use Alfa at all.
React Native - Support React Native out of the box.
Server Render - Support isomorphic app out of the box.
Production Ready - 100% test coverage and being used in productions.
Alfa converts your regular React component into a dependency injected component by injecting application data from a key/value store. Let Alfa handle the data if you use it in different components:
// hello.jsimportReactfrom'react'import{subscribe}from'alfa'// A stateless functional component.functionHelloMessage(props){// Data is injected as the property of props.return<div>Hello ${props.name}!</div>}exportdefaultsubscribe(HelloMessage,['name'])
subscribe makes a new component which gets the variable name and feeds it into the HelloMessage as props.name on (re)rendering.
Now let's see how to use the above component in our app:
// index.jsimportReactfrom'react'import{render}from'react-dom'import{provide,subscribe}from'alfa'importHelloMessagefrom'./hello.js'// Define the root app which renders HelloMessage as a child.constApp=()=>(<div><HelloMessage/></div>)// Create the Root component by wrapping the App component with initial data// using `provide(Component, data)`.constRoot=provide(App,{name:'Motoko'})// Render it!render(<Root/>,document.getElementById('root'))
You don't need to pass the variable name to HelloMessage as alfa gets it from the store and pass it to HelloMessage component automatically. It allows us to quickly move the component around without worrying about how to get the data it depends on.
Changing Data
The simplest way to modify the data of the Alfa store is to inject the built-in set function to the component.
// change.jsimport{subscribe}from'alfa'importReact,{Component}from'react'// A stateful class component.classChangeNameextendsComponent{handleChange=event=>{// Calling `set('mykey', 'my value')` will change the data `mykey`// in store to value `my value`.this.props.set('name',event.target.value)}handleSubmit=event=>{event.preventDefault()}render(){return(<formonSubmit={this.handleSubmit}><label>
Name:
<inputtype="text"value={this.props.name}onChange={this.handleChange}/></label></form>)}}exportdefaultsubscribe(ChangeName,['set','name'],['name'])
As mentioned earlier Alfa makes things explicit. So we need to define the output (the 3rd argument when calling function subscribe) of the component explicitly if we want to change a value of a key in the data store. Otherwise, Alfa complains we are trying to use set without defining the correct output.
Now add the ChangeName component to App and your index.js ends up like this:
// index.jsimportReactfrom'react'import{render}from'react-dom'import{Provider,subscribe}from'alfa'importHelloMessagefrom'./hello.js'constApp=()=>(<div><HelloMessage/><ChangeName/></div>)// Alternatively, you can use Provider - the declarative interface of the// provide function.render(<Providerdata={{name:'Motoko'}}><App/></Provider>,document.getElementById('root'))
Now each time you make a change in the input box React will rerender both HellowMessage and ChangeName components to reflect the change.
You can find the finished version of the above example in the folder examples/hello.
That's an excellent question. You can first try Alfa for your side projects and see if you have any problems when using it. Please feel free to create any issues on GitHub - suggestions, bugs or everything is welcomed.
Hey Agustin!
I am currently working on building a platform where people can post their projects and interested developers can take up those projects. Please sign up if you are interested.
Website: hacklyst.com
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Looking for users, testers and contributors.
Project link: github.com/lsm/alfa
Alfa
Why Alfa?
React is an excellent library for creating interactive and stateful views. However, things become unclear when you need to
share & change data across components
.Alfa is an intuitive and straightforward way to manage React state. It completely decouples the complex relationships between components and let you focus on making components that work anywhere.
Its simple design allows you to adopt it in a matter of minutes while at the same time provides your essential tools to keep your application code easy to change and understand. Here is a list of things why it is the perfect fit for your next React app:
Links
Quick Guide
Installation
Use
npm
to add it to your package.json.Alternatively, use
yarn
if you prefer:Getting Data for Components
Alfa converts your regular React component into a dependency injected component by
injecting
application data from a key/value store. Let Alfa handle the data if you use it in different components:subscribe
makes a new component which gets the variablename
and feeds it into theHelloMessage
asprops.name
on (re)rendering.Now let's see how to use the above component in our app:
You don't need to pass the variable
name
toHelloMessage
as alfa gets it from the store and pass it toHelloMessage
component automatically. It allows us to quickly move the component around without worrying about how to get the data it depends on.Changing Data
The simplest way to modify the data of the Alfa store is to inject the built-in
set
function to the component.As mentioned earlier Alfa makes things explicit. So we need to define the
output
(the 3rd argument when calling functionsubscribe
) of the component explicitly if we want to change a value of a key in the data store. Otherwise, Alfa complains we are trying to useset
without defining the correctoutput
.Now add the
ChangeName
component toApp
and yourindex.js
ends up like this:Now each time you make a change in the input box React will rerender both
HellowMessage
andChangeName
components to reflect the change.You can find the finished version of the above example in the folder examples/hello.
Please check documentation for API and advanced usages.
License
MIT
How can I contribute? Are there issues to start?
That's an excellent question. You can first try Alfa for your side projects and see if you have any problems when using it. Please feel free to create any issues on GitHub - suggestions, bugs or everything is welcomed.
Hey Agustin!
I am currently working on building a platform where people can post their projects and interested developers can take up those projects. Please sign up if you are interested.
Website: hacklyst.com