Wasteland
Wasteland

Reputation: 5389

react-native redux - return part of state

This object on the image is the whole state object. I'd like to create an action that would extract B1-B6 depending on the draw number.

I started it as follows:

action:

export const getDraw = (drawNumber) => {
  return {
    type: GET_DRAW,
    drawNumber
  };
};

reducer:

export default (state = [], action = {}) => {
  switch (action.type) {
    case GET_DATA:
      // returns the whole numbers object
      return action.payload;
    case GET_DRAW:
      return [...state.numbers, ????];
    default:
      return state;
  }
};

The reducer would have to match the drawNumber in an array of objects and then return it alongside with corresponding B1-B6 values.

I'm lost how to do it. Please advise. enter image description here

Upvotes: 1

Views: 61

Answers (1)

Adam Terlson
Adam Terlson

Reputation: 12730

It looks like you want to use a selector rather than an action for this.

Selectors are given the entire store state, some (optional) parameters, and return a value.

In your case, a selector might look something like this (though it's not quite clear in your question how the selection of B1-6 is made based on the draw number):

export const getDraw = (state, drawNumber) => {
  return state.numbers[drawNumber];
};

Generally speaking, actions are used to manipulate state, and selectors are used to retrieve a part of it. It's an anti-pattern to have any actions contain the word "GET" in them because, by definition, every action is a "set" operation on state.

More on actions

More on selectors

Upvotes: 1

Related Questions