aragon / aragon.js

(Aragon 1) A monorepo of JavaScript libraries for interacting with Aragon
https://aragon.org
GNU Affero General Public License v3.0
82 stars 58 forks source link

Question: state within App is disconnected from state within script #323

Closed Schwartz10 closed 5 years ago

Schwartz10 commented 5 years ago

I think this is more of a question/clarification with the react-api docs rather than a bug report.

From this example in the aragon react-api docs, it's suggested that the AragonApi react component takes a reducer prop. My understanding is that the reducer function can hear of state updates from the background script.js, and dispatch state updates to persist (why else would you return a new state?). AKA, if I return a new state from the AragonApi reducer, the next state as seen in the AragonApi and the script.js file should represent that new state. I'm currently running "@aragon/api-react": "^1.0.0-beta.1" and this is not the case.

A more deatiled example:

Initially when my application loads, the reducer in the front-end code (index.js) fires with a state of null, as expected. Following the docs, if (state === null) I return an initialState. However, upon subsequent calls to this reducer, the state is still null. The initialState is not set in the script.js file either. The only time the state variable updates in the reducer (passed to the AragonApi), is when the script.js reducer returns a new state after the INITIALIZATION_TRIGGER event gets fired.

To make things clearer, I have 2 opinions on this:

  1. This is a bug, and the reducer passed to <AragonApi /> should be able to dispatch updates to the global app state
  2. The prop passed to <AragonApi /> is actually read-only - it can't dispatch new updates.

My intuition is that number 2 is the desired behavior - because no event is passed to the reducer either, making it very difficult to properly update state. Thanks for any help and let me know if i can clarify anything further!

welcome[bot] commented 5 years ago

Thanks for opening your first issue in aragonJS! Someone will circle back soon ⚡

sohkai commented 5 years ago

Ah yes, this is an interesting misconception I haven't really thought about (and likely @bpierre too).

You are correct that 2. is the desired behaviour! Wondering if you might be able to patch up @aragon/api-react's readme to make this a bit more clear?

The flow of data is always meant to be

Frontend components

         ^
         |

api.reducer (frontend; api-react)

         ^
         |
       cached
         |

app.store() (script; api)

This additional frontend reducer is meant to take the cached state from the script and marshall it into a form that is friendly to the frontend's components (e.g. converting dates to Date objects, number strings to BN objects or native numbers, etc.)