vitalets / react-native-extended-stylesheet

Extended StyleSheets for React Native
MIT License
2.93k stars 132 forks source link

Use colors object inside EStyleSheet.create #173

Open NeRo8 opened 2 years ago

NeRo8 commented 2 years ago

I have dumb component, and for getting access to colors I must use EStyleSheet.value.

const DumbComponent = () => {
  const [isActive, setActive] = useState(false);

  //Current realisation
  const activeBackground = EStyleSheet.value('$red');
  const disabledBackground = EStyleSheet.value('$blue');

  return (
    <BumbElement color={isActive ? activeBackground : disabledBackground} />
  );
};

But can we put inside EStyleSheet.create custom object with colors what we need, and after use it in our component?

const styles = EStyleSheet.create({
  colors: {
    red: '$red',
    blue: '$blue',
  },
  contentContainerStyle: {
    flexDirection:'row'
  }
});

const DumbComponent = () => {
  const [isActive, setActive] = useState(false);

  return <BumbElement color={styles.colors[isActive ? 'red' : 'blue']} />;
};