Skip to content

Conversation

@francoischalifour
Copy link
Contributor

This simplifies the signature of the store and the reducer, to be more conformed to the state reducer pattern (see React's useReducer as example):

  • Store
    • Remove this.state and relies on scoped state variable, non-accessible from the outside
  • Reducer
    • Only accept (state, action) => state
    • An action is now defined as { type, payload, props } where props are the options passed to Autocomplete

This standardizes the state reducer signature if we were to make this a public API later.

@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 4606a7a:

Sandbox Source
algolia/autocomplete.js: js Configuration

// Arrow down.
event.preventDefault();

store.send(event.key, { shiftKey: event.shiftKey });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this never used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. I initially supported Shift to jump items 5 by 5 but then decided that it was gadget. I forgot to remove this parameter.

Copy link
Contributor

@Haroenv Haroenv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good though!

state.highlightedIndex,
getItemsCount(state),
props.defaultHighlightedIndex
action.props.defaultHighlightedIndex
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason these variables are nested in action.props, and not top-level in action, for an easier type signature for action?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI defaultHighlightedIndex is an Autocomplete option.

Did you mean to not retrieve this value from props but rather from payload.defaultHighlightedIndex?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't get that props meant options, it makes sense in that case then!

return {
...state,
highlightedIndex: props.defaultHighlightedIndex,
highlightedIndex: action.props.defaultHighlightedIndex,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eg. this could maybe just be the payload?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my mind it's still not totally clear what should be passed in our payload when thinking of it as an API. Can you explain the reasoning behind your suggestion?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really use the reducer pattern so often, but saw action.type as the identifier, and then in the cases itself, I usually only saw payload being used, so when writing the dispatch, you don't have to think whether to use payload or props or something else.

Props of course doesn't make sense to think about, since those are the "global arguments", which makes sense as its own thing indeed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, merging as is then!

@francoischalifour francoischalifour merged commit f62c084 into next Oct 12, 2020
@francoischalifour francoischalifour deleted the refactor/core-state-reducer-signature branch October 12, 2020 11:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants