sibs-projects / react-native-redux-saga

Just a starter code to use redux and react-native with Login
178 stars 45 forks source link

need some help #4

Closed nomoreboredom closed 7 years ago

sibelius commented 8 years ago

@nomoreboredom what ur problem? I'll be glad to help :)

nomoreboredom commented 8 years ago

Ok here I go :) First of all thanks for sharing your code, it is the best seed project for react native to my opinion, as it includes every essential element minimalistically.

Rather than forking, I implemented the same methodology(?) you have followed to the app I am writing. Meaning on the root I provide the store, then have the sagas, actions and reducers in the similar folder structure. However, all these concepts are pretty new to me and I have trouble extending the application further, therefore I wanted to ask if you can enlighten me on this.

First of all, on implementation, I did not experience any problem at all, except this line on Init Saga:

yield call(Actions.main);

I believe yield call method expects argmns. However if I comment out that line, the app still works as it loads the storage properly and checks the token. Was your intention there to route to a Scene with key "main" from flux?

Currently, I can bind storage data (counter, or user data) to show on containers, redux connect solves it. However, for instance, after I login, I would like to use that token to make more api calls and get my data (For instance another token, I have to use double token. I would like to use the login token and from the api get a second token, and pass its value to user object on the store)

What would be the proper way of doing that? Should I add another saga to run after login saga to run? If that is the case, can you perhaps show an example how I may pass the token I have retrieved from login saga to this new saga?

Additionally, partly related, what would be your way to implement the api calls in this? After I get the tokens I ll be able to pull data via fetch in componentWilMount functions in containers, but somehow I feel that is not the optimal way..I mean the data I use in my app, not store data necassary. Should I define actions and reducers for the methods in rest api, and invoke them from the containers via dispatches, even they are not in storage?

Thanks again for sharing your code!

sibelius commented 8 years ago

you should store the token inside your reducers, maybe inside user.token, then u can retrieve it inside your saga

yield select(getToken());

you can use fetch to implement api calls, https://github.com/crocodilejs/frisbee could help you as well

Example of request using a saga

function *watchCustomerGetRequest(action) {
  const { id } = action;
  try {
    //
    const res = yield call(api.get, endpoints.customer(id));

    if (res.err) throw res.err;

    const data = normalize(res.body, Schemas.CUSTOMER);

    yield put(customerAcceptSuccess(data));
  } catch (err) {
    if (err.message === 'Invalid token') {
      yield put(logout());
    } else {
      yield put(customerAcceptFailure(err.message));
    }
  }
}