Comments (10)
exactly the point! Rule of thumb is: if your action is async use the store methods, otherwise just use the state parameter from the action.
from redux-zero.
Although thinking about this example, maybe it would be nice to perform something like this:
const getActions = ({ setState }) => ({
async getTodos() {
try {
setState({ loading: true });
const payload = await client.get("/todos");
return { payload, loading: false };
} catch (error) {
return { error, loading: false };
}
}
});
And this kind of action, probably have a better testability. It could be done roughly like this:
describe("todo actions", () => {
let actions, store, listener, unsubscribe;
beforeEach(() => {
store = createStore();
actions = getActions(store);
listener = jest.fn();
unsubscribe = store.subscribe(listener);
});
it("should fetch todos", async () => {
nock('http://someapi.com/')
.get('/todos')
.reply(200, { id: 1, title: "test stuff" });
await actions.getTodos();
const [LOADING_STATE, SUCCESS_STATE] = listener.mock.calls.map(([call]) => call);
expect(LOADING_STATE.loading).toBe(true);
expect(SUCCESS_STATE.payload).toEqual({ id: 1, title: "test stuff" });
expect(SUCCESS_STATE.loading).toBe(false);
});
});
@matheusml, your opinion please.
from redux-zero.
I try to keep actions as pure as posible (state in / state out) and manage the side actions in the component with something like this to keep component as clean as posible
import { config } from 'http-decorator';
config.base = 'http://jsonplaceholder.typicode.com';
...
class SomeClass {
@http('put')
public someMethod (url: string, params?: any, error?, result?): void {
// the actual body method will be executed after request was performed
if (error) throw error
... do something with the result
}
}
...
someClassInstance.someMethod('users');
// the same as doing
// $ curl -X PUT http://jsonplaceholder.typicode.com/users
Hope this may help.
from redux-zero.
Hi @seveves
That's a good question. Right now we don't have the full documentation, but we have some examples on dealing with async functions.
But, I think that your example is 100% fine.
@malbernaz have something to add here?
from redux-zero.
@k1r0s and @matheusml ... thank you for your answers!
I want to remove all the async http logic from my components to keep them as simple as possible but of course the actions should stay as pure as possible. As always you have to compromise. And redux-zero should stay small and simple so it's ok that the users have to find a way to go with.
But yeah ... maybe it would be helpful to have something about side effects in the documentation 👍
When @malbernaz has nothing to add here then feel free to close this "issue".
from redux-zero.
@seveves you are thinking as if you were using redux... In redux-zero we expose the store in the actions function to better support async. You don't need to have three actions to handle a fetch request, you can do something like this:
const actions = ({ getState, setState }) => ({
fetch: state => {
fetchFromApi()
.then(payload => {
setState({ payload, loading: false });
})
.catch(error => {
setState({ error, loading: false });
})
return { loading: true };
}
});
from redux-zero.
so const actions = store => ({ ... })
is the same as const actions = ({ getState, setState }) => ({ ... })
? Not needing the three actions sounds very nice :)
from redux-zero.
If you have more questions @seveves feel free to ask.
For now, I'm closing this 👍
from redux-zero.
Since async/await is what the cool kids are doing, and the readability for this case is better, I think we should do it 👍
from redux-zero.
none of the examples above seem to be working :s
Only Loading get's updated, but the other state attributes don't
from redux-zero.
Related Issues (20)
- redux devtools dispatch not working HOT 2
- Adding redux-devtools options parameters HOT 1
- Writing typesafe actions HOT 1
- Unit mapToProps? (pass through) HOT 1
- Release seems to be missing types/Actions.ts HOT 5
- Use via script tags? HOT 1
- react waning for <Connect> HOT 11
- Not able to use redux-zero through UMD HOT 1
- Action type doesn't support Promise as return type HOT 1
- RFC - Remove all current binding functions except for Hooks HOT 10
- Add useActions hook for react HOT 2
- Update connect/Connect to use hooks instead of classes
- How to ensure the sequence of the async actions? HOT 2
- where is getActions exported from? HOT 1
- Proper syntax for store.setState callback?
- Docs website down?
- The latest published `types/Actions.d.ts` is not same as `types/index.d.ts` . HOT 2
- value from props is not passing
- use redux-zero in typescript HOT 1
- Can't test redux-zero with JEST and TypeScript? HOT 2
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 redux-zero.