1

I have an object and I need to return each one to check for its mapping result. My problem with my code below is on this line

style: { ...map[settings] },

I need a way to go through each setting and pass it to map. Not sure how to go about that the most efficient way in es6 ?

This is the value of settings been passed into the PassThrough component

const settings = {
  block_background: GradientGreen,
  block_spacing: padding,
};

Here is my Component

const PassThrough = ({ children, settings }) => {
  const map = {
    GradientGreen: {
      background: 'linear-gradient(-95deg, green, blue 100%)',
    },
    padding: '20px',
  };

  const cloneChild = () => {
    return cloneElement(children, {
      style: { ...map[settings] },
    });
  };

  return (
    <Fragment>
      {cloneChild()}
    <Fragment/>
  );
};

1 Answer 1

2

The shortest is probably:

 inlineStyle: Object.assign({}, ...Object.entries(settings).map(([k, v]) => ({ [k]: map[v] })))

But actually I would change settings to:

const settings = original => {
 block_background: original.GradientGreen,
 block_spacing: original.padding,
};

Then it is just:

inlineStlye: settings(map)
Sign up to request clarification or add additional context in comments.

6 Comments

I can't change settings its coming from a server. This seems to be returning the incorrect values. Seems to be returning the settings object and not the map values
@me-me what output do you expect?
You're code is right it was mine that was off. Thank you for this its exactly what I'm after
I changed your code a little to work with what I'm after. But the issue is the values are coming back as an object with keys associated to them. I'm looking to combine both into one. I feel like I'm better off starting another thread based on the new code.
@me-me whoops, fixed that
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.