Giter Site home page Giter Site logo

ruben-arushanyan / react-component-shell Goto Github PK

View Code? Open in Web Editor NEW
10.0 1.0 0.0 3.85 MB

react-component-shell is a package that allows you to quickly and easily create react-contexts and implement state management.

Home Page: https://react-component-shell.js.org

License: MIT License

JavaScript 78.33% CSS 21.45% HTML 0.22%
react react-component-shell context react-context reactjs component-shell context-api state-management easy-context react-context-state

react-component-shell's Introduction

You can find the full documentation on the website

react-component-shell is a package that allows you to quickly and easily create react-contexts and implement state management.

Shell is a JavaScript class that has certain methods and properties to provide some type of functionality in the project.

The main concept is to create shell objects and connect them to react components.

Installation

npm install react-component-shell

Basic Usage

Let's create a Game shell that has two methods: run() and stop() that update the .paused property of the state.

game.js

import {Shell} from 'react-component-shell'

class Game extends Shell {
   state = { paused: true }

   run() {
      this.updateState(state => {
          return {...state, paused: false}
      })
   }

   stop() {
      this.updateState(state => {
          return {...state, paused: true}
      })
   }
}

export {Game}

Now let's use the createShellProvider() function to create a react-context provider and access hooks for the Game shell.

game-context.js

import {createShellProvider} from 'react-component-shell'
import {Game} from './game.js'

const [ GameProvider, useGame, useGameState ] = createShellProvider({ shellClass: Game })

export {GameProvider, useGame, useGameState}

The createShellProvider() function returns an array with three values. The first value is a provider component, the second value is a react hook that returns a shell object, and the last value is a react hook that return a state value by a selector.
In our example, we created the GameProvider provider and useGame, useGameState hooks.

Now let's use them in react app.

App.js

import {GameProvider, useGame, useGameState} from './game-context.js'

const App = (props) => {
    return (
        <GameProvider>
            <GamePauseButton />
        </GameProvider>
    )
}

const GamePauseButton = (props) => {
    const game = useGame()
    const paused = useGameState(state => state.paused)

    const clickHandler = () => {
        if (paused) {
            game.run()
        } else {
            game.stop()
        }
    }

    return <button onClick={clickHandler}>{ paused ? 'Run' : 'Stop' }</button>
    
}

export default App

In the example above, we can apply the useGame() or useGameState() hooks to any component inside the <GameProvider>.

useGame() returns a game object, and we can call its methods run() or stop() or read and write its properties.

useGameState(selector) returns the value of the state of the game, which is indicated by the selector function, and every time the change of the specified value in the state will result in the re-rendering of the given component.



Read our contributing guide to learn about our development process.

This project has adopted the Contributor Covenant as its Code of Conduct, and we expect project participants to adhere to it. Please read the full text so that you can understand what actions will and will not be tolerated.

Authors

License

MIT License

react-component-shell's People

Contributors

ruben-arushanyan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

react-component-shell's Issues

v4.0.0

New Approach, new API

Checklist

  • Implementation
  • Demo test
  • README docs
  • Manual Test using README docs

example:

import {Shell, createShellProvider} from 'react-component-shell'

class Game extends Shell {
   state = { paused: true }
    
   run() {
      this.updateState(state => {
          return {...state, paused: false}
      })
   }
    
   stop() {
      this.updateState(state => {
          return {...state, paused: true}
      })
   }

}

const ProviderWrapper = (props) => {
   const {shell, children} = props
   
   return children
}

const [GameProvider, useGame, useGameState] = createShellProvider({
    shellClass: Game,
    customProviderWrapper: ProviderWrapper
})

useStoreState

import { useEffect, useState } from 'react'

const useStoreState = (store, selector) => {
    const [value, setValue] = useState(selector(store.state))

    useEffect(() => {
        setValue(selector(store.state))
        return store.subscribeSelector(selector, (value) => {
            setValue(value)
        })
    }, [selector, store])

    return value
}

export { useStoreState }

shell prop

If you need access to the shell outside of a react component

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.