Categories
JavaScript Answers

How to set view width automatically by text inside with React Native?

Spread the love

Sometimes, we want to set view width automatically by text inside with React Native.

In this article, we’ll look at how to set view width automatically by text inside with React Native.

How to set view width automatically by text inside with React Native?

To set view width automatically by text inside with React Native, we set the alignSelf style.

For instance, we write

<View style={{ backgroundColor: "#000000", alignSelf: "flex-start" }}>
  <Text style={{ color: "#ffffff" }}>Sample text here</Text>
</View>

to set the alignSelf style of the View to 'flex-start' to make it display as an inline-block component.

An inline-block component will be sized according to the width of its contents.

Conclusion

To set view width automatically by text inside with React Native, we set the alignSelf style.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *