Monday, 1 October 2018

Can I select all react components of a type, without assigning a class to each one?

I have a Playground here: https://codesandbox.io/s/736v9vjzw1

const Something = ({ classes, children, variant }) => {
  return (
    <div className={classes.someThing}>
      <p> I'm some thing </p>

      <SomeOtherThing />

      <SomeOtherThing> I have some children </SomeOtherThing>

      <SomeOtherThing> I have some children </SomeOtherThing>

      <SomeOtherThing> I have some children </SomeOtherThing>
    </div>
  );
};

const styles = {
  someThing: {
    color: "green",
    border: "solid 2px black",
    margin: 30,

    "& $someOtherThing": {
      backgroundColor: "pink" // Doesn't work
    },

    "& p": {
      fontWeight: "bold" //This works but is too broad
    }
  }
};

I have a situation here, where I want to style all the SomeOtherThings inside my SomeThing.

I can use & p selector to select the p element - but I don't like this. It would style any random ps I have around - and I don't want to have to look inside the component definition to find what it's top level element is.

How can I do this? Something like & SomeOtherElement.

The real world application of this, is that in some places I want have SomeOtherElement be displayed block and other places inline-block.



from Can I select all react components of a type, without assigning a class to each one?

No comments:

Post a Comment