Why use Stylesheet.create?

Asked

Viewed 334 times

1

Why use the StyleSheet.create? When starting a new project (react-native init test, the project comes with the style as follows.

const styles = StyleSheet.create({
  container: {

  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
  Imagem: {
    resizeMode: 'contain',
    width: 50,
    height: 50
  }
});

But if I do it this way, the style will work the same way, so what’s the difference?

const styles = {
  container: {

  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
  Imagem: {
    resizeMode: 'contain',
    width: 50,
    height: 50
  }
}

1 answer

2

I think I’d better use StyleSheet whenever possible because you will have more performance than just using common objects.

Quote removed from React documentation :

Create a stylesheet from a style object (Stylesheet) makes it possible to refer to it by ID instead of creating a new style object every time.

Him(Stylesheet) also allows sending style only once. All subsequent uses will forward an id (not implemented yet).

I hope it helps!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.