Code Monkey home page Code Monkey logo

deku-stateful's Introduction

deku-stateful

Keep state in Deku components

Deku v2 has no states in components. This is a higher-order component that adds state and setState to the model. See this conversation here.

Compatible with Deku 2.0.0 (tested with 2.0.0-rc11) and Decca 2.0.0.

Status

import stateful from 'deku-stateful'

function initialState () {
  return { clicked: 0 }
}

function render ({ getState, setState }) {
  return <div>
    Clicked { getState().clicked } times.
    <button onClick={ () => setState({ clicked: getState().clicked + 1 }) }>
      Click me
    </button>
  </div>
}

export default stateful({ initialState, render })

Example

API

render, onCreate, onUpdate, onRemove

The render function and the lifecycle hooks will also be passed getState and setState.

function render({ getState, setState }) {
}
  • setState(object) — Updates the state when called. When setState is ran, it will queue up changes and dispatch an event like dispatch({ type: 'UI_STATE_CHANGE' }). This is meant to be picked up by your Redux store, which we're assuming will retrigger a render() when called.
  • getState() — Returns the current state.
  • state — The current state; it's preferred to use getState() instead, but it's here for legacy compatibility.

initialState

Your component can have an initialState function. Return the first state here.

function initialState ({ props }) {
  return { clicked: false }
}

export default stateful({ initialState, render })

Thanks

deku-stateful © 2016+, Rico Sta. Cruz. Released under the MIT License.
Authored and maintained by Rico Sta. Cruz with help from contributors (list).

ricostacruz.com  ·  GitHub @rstacruz  ·  Twitter @rstacruz

deku-stateful's People

Contributors

11111000000 avatar borisirota avatar rstacruz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

deku-stateful's Issues

onCreate

there is no onCreate hook decorator

getState

to write asyncronous code inside controller:

export default stateful( {
   render({ state, setState }) {
       return (<SomeComponent doAsynJob={ someFunc }></div>)
       function someFunc() { /* `state` here is the state when doAsynJob is initialized, not the actual state */ }
   } 
}

Something wrong (not sure)

Here: https://gist.github.com/c570a5a3d60aa62d7918 gist, containing Tabs implementation with deku-stateful. It is working as is in deku 1.0. On Tabs first show - all works ok, but on second shown in the same place, we have { state } === undefined inside render fn. Looks like here we have same path (0.0.1.0.0.0.0), and it is destroyed onRemove inside stateful decorator.

pass state to onRemove lifecycle event as well

Hi, in my use case I'm using deku-stateful to store local state for modal component. I'm attaching keydown and click events to the document on creation (support closing the modal on esc and when clicking outside) and storing the listeners inside the state in order to remove them when the component is being removed.

Currently, I'm not able to use deku-stateful because its not giving me the option to clean up this kind of resources on remove.

Thanks !

Bug with lists?

Demo fiddle: https://jsfiddle.net/montlebalm/m6mkac75/1/

I may be misunderstanding how this is supposed to work, but it seems like there's a bug with lists of components. Specifically, state is incorrectly preserved when a component is removed.

Try these steps in the linked example:

  1. Click "Hit me!" on each of the counters to generate a unique count. For the sake of argument I'll assume the first has a count of 1, the second 2, and the third 3.
  2. Click the "X" button on the first counter
  3. Verify that there are now 2 counters and the counts are 1 and 2 respectively

I would expect the counts to be 2 and 3 given that we removed the first counter. It's easy to see that the path of the first counter remained the same, which means it inherited the state from the removed counter.

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.