Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 719
Zack Shapiro Avatar asked Mar 01 '26 19:03

Zack Shapiro


1 Answers

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),
  };
};
like image 52
Tomasz Mularczyk Avatar answered Mar 03 '26 10:03

Tomasz Mularczyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!