Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating helper functions is unintuitive #114

Open
coffee-cup opened this issue Sep 26, 2019 · 0 comments
Open

Creating helper functions is unintuitive #114

coffee-cup opened this issue Sep 26, 2019 · 0 comments
Labels
question Further information is requested

Comments

@coffee-cup
Copy link
Collaborator

coffee-cup commented Sep 26, 2019

Sometimes when writing actions or components, it can be a good idea to split the logic that accesses the universe into reusable functions. For example, if I have the following few lines throughout my code,

import { state } from "./model";

const myAction = () => {
  const currentGameId = state.currentGame;
  const game = state.games[currentGameId];
 
  // ...
}

I may want to extract it to a function

import { state } from "./model";

const getCurrentGame = () => {
  const currentGameId = state.currentGame;
  const game = state.games[currentGameId];
  return game;
}

const myAction = () => {
  const game = getCurrentGame();
}

However, this does not work. The transpiler recognizes that the getCurrentGame function uses the state and turns in into an action. To make this work I need to pass the state varaible.

import { state } from "./model";

const getCurrentGame = (state) => {
  const currentGameId = state.currentGame;
  const game = state.games[currentGameId];
  return game;
}

const myAction = () => {
  const game = getCurrentGame(state);
}

This is fairly unintuitive because state is imported at the file level, but then needs to be passed from the action to a helper function. The example above is in an action, but the same idea applies to accessing and watching state in a component.

I am not exactly sure how to fix this, but I expect some users will get confused by this. I know @andrejak encountered this when developing the devtools, and I have encountered when just writing example apps.

Any thoughts on the above @Onurbon?

@coffee-cup coffee-cup added the question Further information is requested label Oct 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant