Zack Shapiro
Zack Shapiro

Reputation: 6998

mapStateToProps with an ID?

Right now I have a generic function that gets all Elements

const mapStateToProps = ({elements}) => {
    return {
        elements: getElementsByKeyName(elements, 'visibleElements'),
    };
};

I'd like to change it to something along the lines of

const mapStateToProps = ({elements}) => {
    return {
        elements: getElementsById(elements, this.props.elementId),
    };
};

If I'm initializing my React class with a required elementId prop, is possible to achieve this rather than getting them all and filtering every time I need to be more specific in my view?

Thanks!

Upvotes: 3

Views: 434

Answers (1)

Tomasz Mularczyk
Tomasz Mularczyk

Reputation: 36179

the mapStateToProps function takes the second argument which is own props(props passed to a component outside connect). You can grab the id that way if I correctly understood what you're trying to achieve.

const mapStateToProps = ({elements}, {id}) => {
  return {
    elements: getElementsById(elements, id),
  };
};

Upvotes: 3

Related Questions