Karina Shulan
Karina Shulan

Reputation: 159

How to inherit and overide object in JS

I have a react StyleSheet with objects

  const styleSheet = StyleSheet.create({
    picture1: {
      height: 50,
      width: 30,
      alighItems: 'center',
    }
    picture2: {
      height: 50,
      width: 50:,
      alighItems: 'center',
    }
  })

My question is : How do I assign properties of picture1 to picture2, and ovveride width to 50?

So I could write just picture2: {width: 50} without having to rewrite same code?

I tried this way but unfortunately it didn't work.

 const styleSheet = StyleSheet.create({
    picture1: {
      height: 50,
      width: 30,
      alighItems: 'center',
    }
    picture2: {
      ...picture1,
      width: 50:,
    }
  })

Upvotes: 1

Views: 33

Answers (1)

Thomas
Thomas

Reputation: 181815

  const picture1 = {
    height: 50,
    width: 30,
    alignItems: 'center', // Note: alignItems not alighItems!
  }
  const styleSheet = StyleSheet.create({
    picture1,
    picture2: {
      ...picture1,
      width: 50,
    }
  })

Upvotes: 2

Related Questions