Comments (3)
Same issue here, when try to compose 2 providers with the same component:
const counterStore = provideState({
initialState: () => ({counter: 0}),
effects: {
addCount: (effects, val) => state => Object.assign({}, state, {counter: state.counter + val})
}
});
const colorStore = provideState({
initialState: () => ({color: 'black'}),
effects: {
setColor: (effects, col) => state => Object.assign({}, state, {color: col})
}
});
const CounterChildComponent = injectState(({state: {color, counter}, effects}) => {
const handleClick = () => effects.addCount(1);
return (
<div className="childComponent" onClick={handleClick}>
<span>CounterChild ...</span>
<span>color: {color}, {counter}</span>
</div>
);
});
const ColourChildComponent = injectState(({state, effects}) => {
const handleClick = () => effects.setColor('blue');
return (
<div className="childComponent" onClick={handleClick}>
<span>ColorChild ...</span>
<span>{state.color}</span>
</div>
);
});
const ParentComponent = (props) =>
<div className="parent">
<CounterChildComponent/>
<ColourChildComponent/>
</div>;
export default counterStore(colorStore((ParentComponent)));
// ramda compose does not work either ->
// R.compose(counterStore, colorStore)(ParentComponent)
It's strange because when apply the colorStore in first place, you can update the colourChild but not the counterChild ...
... if you click on counterChild many times and then click on the color component, both states are updated
from freactal.
@jhsu, there's one problem with the code you posted, and that's probably the source of the problems you're encountering. The state update function is a synchronous function - it returns new state. However, your nextPage
state update function returns a Promise
. That's probably not what you intended. Something like this will work:
const serverData = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const getData = page => {
page = page || 0;
return Promise.resolve(serverData[page]);
};
const withFetching = provideState({
initialState: () => ({ entities: [0] }),
effects: {
fetchPage: async (effects, page) => {
const pagedData = await getData(page);
return state => {
const entities = [].concat(state.entities);
entities[page] = pagedData;
return Object.assign({}, state, { entities });
};
}
}
});
const justPagination = provideState({
initialState: () => ({ page: 0 }),
effects: {
nextPage: async (effects, currentPage) => {
const nextPage = currentPage + 1;
await effects.fetchPage(nextPage);
return state => Object.assign({}, state, { page: nextPage });
}
}
});
const App = injectState(({ state, effects }) => {
const onClick = () => effects.nextPage(state.page);
return (
<div>
<div>entities: { JSON.stringify(state.entities) }</div>
<div>page: { state.page }</div>
<button onClick={onClick}>click</button>
</div>
);
});
from freactal.
@TIMNHE, your issue is somewhat different. I believe it is fixed here and was the same bug as #42. This will be released on npm
shortly.
from freactal.
Related Issues (20)
- state and effects
- middleware not executing in Jest
- Calculated states giving me the blues HOT 1
- A State Factory for Freactal
- Freactal doesn't work well with recursive state provider components. HOT 13
- A "Freactal Manager" module: Freactal Seed HOT 3
- effects.initialize executed version is unpatched through middleware.
- Putting up a Starter Kit with Freactal
- effects can't access parent scope
- Jumping cursor with controlled input HOT 1
- Looking for a new maintainer! HOT 15
- An alternative: looking glass engine
- Type codebase with flow HOT 3
- Any intention to support decorators? HOT 6
- effects with async/await functions HOT 4
- Published package (v2.0.0) is out of sync with the actual code? HOT 15
- (Re-)Publish Freactal intro on Hacker Noon? HOT 3
- React Native database? HOT 2
- Injecting new state asynchronously HOT 6
- middleware
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 freactal.