Comments (9)
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.
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.
@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.
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.
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.
Thanks addMemory() work
I will try helper to separate actions later :)
from unistore.
These are both super useful, much appreciated!
from unistore.
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.
@jsu93 's new version worked for me
from unistore.
Related Issues (20)
- Mutations & More parameters to listeners
- s
- Race in the async action example? HOT 1
- Questions about this lib HOT 1
- run action while componentDidMount do not trigger props update HOT 1
- [feat] Namespacing actions HOT 2
- Remove depracated in react 16.9 componentWillReceiveProps
- connect in React.FunctionComponent? HOT 4
- Web Component Bindings - Feature Request HOT 2
- React type error: Generic type 'FC' requires between 0 and 1 type arguments.
- How to use Unistore action in JEST for shallow testing? HOT 2
- [question] How to best handle chained async actions? HOT 4
- Can't make unistore work with TypeScript HOT 1
- Stale state seems to be written to store when using a recursive object structure HOT 1
- [Lack of Docs !] Where can I find a clear Doc to use it through multiple files ? HOT 2
- Errors with WMR HOT 2
- How to "dynamically" `connect` to different states with React/Preact? HOT 1
- setState not updating components HOT 1
- Multiple Stores Overwrite on last store
- Typings for function binding an action does not allow async
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from unistore.