项目作者: JWebCoder

项目描述 :
React context state management system
高级语言: TypeScript
项目地址: git://github.com/JWebCoder/recost.git
创建时间: 2018-05-13T00:39:26Z
项目社区:https://github.com/JWebCoder/recost

开源协议:MIT License

下载


Recost

React context state management system

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Installing

Use yarn or npm to install the package in your React Application

  1. yarn add recost

Usage

Next you need to initialize the context with a reducer for your application.

index.js

  1. import React from 'react'
  2. import ReactDOM from 'react-dom'
  3. import App from './App'
  4. import initContext, { Provider } from 'recost'
  5. // create a reducer function
  6. const reducer = (state, action) => {
  7. if (action.type === 'COUNT') {
  8. return {
  9. ...state,
  10. count: state.count + 1
  11. }
  12. }
  13. return state
  14. }
  15. // define the initial state for the application
  16. const initialState = {
  17. count: 1
  18. }
  19. // initialize the context
  20. initContext(initialState, reducer)
  21. ReactDOM.render(
  22. <Provider> // add the Provider component to your application
  23. <App></App>
  24. </Provider>,
  25. document.getElementById('root')
  26. )

Now you can use the dispatcher and the state anywhere in the code

App.js

  1. import React, { Component } from 'react'
  2. import { dispatch, withState } from 'recost'
  3. let Count = (props) => {
  4. return <p>{props.count}</p>
  5. }
  6. const mapStateToProps = (state) => ({
  7. count: state.count
  8. })
  9. Count = withState(mapStateToProps)(Count)
  10. class App extends Component {
  11. onClickButton() {
  12. dispatch({
  13. type: 'COUNT'
  14. })
  15. }
  16. render() {
  17. return (
  18. <div>
  19. <Count></Count>
  20. <button onClick={this.onClickButton}>
  21. Increase
  22. </button>
  23. </div>
  24. )
  25. }
  26. }
  27. export default App

Using middleware

Available middleware

recost-persist - persist/hydrate state using localstorage
recost-logger - logs state changes when in development mode

Create your own

In this example we have added two middleware functions.

A logger, that will run before and after the state changes logging the changes.

logger.js

  1. const before = (state, action) => {
  2. console.log('State before action:', state)
  3. console.log('Full action:', action)
  4. }
  5. const after = (state, action) => {
  6. console.log('State after action:', state)
  7. }
  8. export {
  9. before,
  10. after
  11. }

And a callAPI, that runs before each state change.

callAPI.js

  1. const before = (state, action, dispatch) => {
  2. if (action.type === 'COUNT_API') {
  3. setTimeout( // simulates and api call
  4. () => {
  5. dispatch({
  6. type: 'SUCCESS_COUNT'
  7. })
  8. }
  9. , 1000
  10. )
  11. }
  12. }
  13. export {
  14. before
  15. }

Now we just need to add the new middleware to the context

index.js

  1. import React from 'react'
  2. import ReactDOM from 'react-dom'
  3. import App from './App'
  4. import initContext, { Provider } from 'recost'
  5. import * as logger from './Context/logger'
  6. import * as callAPI from './Context/callAPI'
  7. // create a reducer function
  8. const reducer = (state, action) => {
  9. if (action.type === 'COUNT') {
  10. return {
  11. ...state,
  12. count: state.count + 1
  13. }
  14. }
  15. return state
  16. }
  17. // define the initial state for the application
  18. const initialState = {
  19. count: 1
  20. }
  21. // initialize the context with the middleware
  22. initContext(initialState, reducer, [
  23. logger,
  24. callAPI
  25. ])
  26. ReactDOM.render(
  27. <Provider> // add the Provider component to your application
  28. <App></App>
  29. </Provider>,
  30. document.getElementById('root')
  31. )

API definition

initContext

Creates a new application context

  1. import initContext from 'recost'
  2. initContext(initialState, reducer, middleware)
Params required description
initialState yes initial application state
reducer yes function that generates new state based on actions
middleware yes Array of middleware that can run before or after the reducer taking place

initialState

Defaults to an empty object {}

reducer

  1. const reducer = (state, action) => {
  2. if (action.type === 'COUNT') {
  3. return {
  4. ...state,
  5. count: state.count + 1
  6. }
  7. }
  8. return state
  9. }
Params description
state current application state
action action object passed by the dispatcher function

middleware

Defaults to an empty array []

dispatch

Dispatches an action that will trigger a state change

  1. import { dispatch } from 'recost'
  2. dispatch(actionObject)
Params required description
actionObject yes object containing the type of action and the payload if necessary

actionObject

  1. dispatch({
  2. type: 'COUNT',
  3. payload: null // this property is only required if we want to pass in some data
  4. })

withState

Composed function that takes a mapStateToProps function and a component

  1. import { withState } from 'recost'
  2. WrappedComponent = withState(mapStateToProps)(Component)
Params required description
mapStateToProps no function that receives the full state and return a portion of it, if not defined, the entire state is sent to the component
component yes component to wrap with state

mapStateToProps

  1. const mapStateToProps = (state) => ({
  2. count: state.count
  3. })

component

  1. const Count = (props) => {
  2. return <p>{props.count}</p>
  3. }
  4. const WrappedCount = withState(mapStateToProps)(Count)

or

  1. class Count extends React.[PureComponent|Component] {
  2. render() {
  3. return <p>{this.props.count}</p>
  4. }
  5. }
  6. const WrappedCount = withState(mapStateToProps)(Count)

Provider

Provider component, sets where we want to deliver our context

  1. import React from 'react'
  2. import ReactDOM from 'react-dom'
  3. import { Provider } from 'recost'
  4. ReactDOM.render(
  5. <Provider>
  6. <App></App>
  7. </Provider>,
  8. document.getElementById('root')
  9. )

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments