GithubHelp home page GithubHelp logo

Comments (9)

developit avatar developit commented on May 23, 2024 5

To separate actions, here's a little helper function:

function combineActions(...allActions) {
  return store => allActions.reduce( (combined, actions) => {
    for (let i in actions) {
      let action = actions[i];
      if (typeof action==='function') action = action(store);
      combined[i] = action;
    }
    return combined;
  }, {});
}

// usage:

import userActions from './userActions';
import UIActions from './UIActions';
import combineActions from '../util/combine-actions';

export default combineActions(userActions, UIActions);

... where userActions and UIActions are written as you described above.

from unistore.

developit avatar developit commented on May 23, 2024 3

Here's code for HMR:

// adds memory backing to a unistore store instance:
function addMemory(store) {
  if (window.STATE) store.setState(window.STATE);
  store.subscribe( state => {
    window.STATE = state;
  });
}


// example usage:
let store = createStore( ... )
addMemory(store)

from unistore.

jsu93 avatar jsu93 commented on May 23, 2024 2

@developit #81 (comment) didn't work

import userActions from './userActions';
import UIActions from './UIActions';
import combineActions from '../util/combine-actions';

class App extends Component {
	render() {
        console.log(this.props) // no action method there
    	return <div>ok</div>
    }
}
const allActions = combineActions(userActions, UIActions);
export default connect(state => state, allActions)(App)

Did I use the combineActions right with connect ?

from unistore.

lukelindsey avatar lukelindsey commented on May 23, 2024 1

I split my actions in files but kept them as function(store) => object with the functions on there just like shown in store/actions.js in the OP. Not sure if that's the right way to do it, but I got combineActions working like this

function combineActions(...allActions) {
  return store => allActions.reduce( (combined, actions) => {
    let actionObj = actions;
    if (typeof actions ==='function') actionObj = actions(store);
    for (let i in actionObj) {
      combined[i] = actionObj[i];
    }
    return combined;
  }, {});
}

All my action files are following the function(store) style, so I always go in the if, so take the implementation with a grain of salt.

from unistore.

jsu93 avatar jsu93 commented on May 23, 2024 1

@lukelindsey

Yes, I found it, 👍

function combineActions(...allActions) {
  return store => allActions.reduce((combined, actions) => {
    actions = actions(store);
    for (let i in actions) {
      if (combined[i]) throw new Error('Have a repeat action name: '+i)
      let action = actions[i];
      combined[i] = action;
    }
    return combined;
  }, {});
}

from unistore.

azulkipli avatar azulkipli commented on May 23, 2024

Thanks addMemory() work

I will try helper to separate actions later :)

from unistore.

darve avatar darve commented on May 23, 2024

These are both super useful, much appreciated!

from unistore.

JMSantos94 avatar JMSantos94 commented on May 23, 2024

If anyone has issues with devtools not displaying correctly the name of the actions after combining them with combineActions. I added this quick fix:

function combineActions(...allActions) {
    return store =>
        allActions.reduce((combined, actions) => {
            for (let i in actions) {
                let action = actions[i];
                let value = action.name;
                if (typeof action === 'function')
                    action = Object.defineProperty(action(store), 'name', {
                        value,
                    });
                combined[i] = action;
            }
            return combined;
        }, {});
}

My assumption is that: when action(store) is called, it returns a brand new function without a name. Using Object.defineProperty I set the name into this new function. 😄

from unistore.

cmonteiro128 avatar cmonteiro128 commented on May 23, 2024

@jsu93 's new version worked for me

from unistore.

Related Issues (20)

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.