Storybook with React Native on Web and Native Android / iOS components

in #utopian-io5 years ago (edited)

Imagine to define a React UI component that render on the Web Browser in a progressive web app (PWA) or as a Native component on Android or iOS.

This is already possible thanks to React Native Web, it is an Abstraction that supports almost all the React Native features on Web, it also means that you can't use plain CSS to style your UI because Native UI on Android / iOS don't use it.

Another issue is that Web HTML components and Native UI are rendered in different ways so it's very important to frequently test the changes among the different environments and Storybook helps a lot.

What Will I Learn?

  • How to test React Native agnostic components
  • Some consideration about the current ecosystem and Storybook

Requirements

To follow this tutorial you need this software

  • Ubuntu Linux LTS (or another operative system with NodeJS)
  • NPM/yarn packagereact-native-web and storybook
  • Expo app installed on Android or iOS

Difficulty

  • Intermediate

Tutorial Contents

  • What is Storybook and why is important to test component is isolation?
  • Storybook 4 + React Native Web on Web and Native
  • Your first agnostic web component

What is Storybook and why is important to test components in isolation?

Clients want results for their apps, but unfortunatly the more features you add the more it becames harder to debug and to find bugs.

The proper way to maintain software developers mental health, current features, the future improvements, is to build and UI toolkit or at least a catalog with critical UI components in isolation. That means, you can just focus on a component at time without wasting time navigating the app again and again, for every change you make.

Storybook is a tool to do that, the developers can see it as a way to "visual test" the UI component in isolation and the graphic designer can see it as a live catalog of components.

Storybook 4 + React Native Web on Web and Native

The current situation is in evolution. The latest Storybook 5 supports React only, while Storybook 4 supports React and React Native. The easiest way to test Storybook React Native on Web and Native is via react-native-hybrid-storybook but it supports only Storybook 3..

Long story short.. Kiwi built an internal boilerplate that allows to you to just concentrate on your react components. That means maybe You'll have to move your components in future, but there is already a separation between your agnostic components code and the magic needed to render the components in Web, Android and iOS.

Your first agnostic web component

Download the boilerplate Kiwi/Margarita.

git clone https://github.com/kiwicom/margarita status-components-sandbox

I know it's huge, but your code will live just in a specific directory inside packages/, lets say packages/status-components.

yarn
cd packages/status-components

Good. Now You have all the dependencies installed, and you can start to develop you Hello World component, an agnostic button.

It seems just a boring button but the magic here is that Btn will use react-native on Android/iOS and react-native-web when it will be rendered in the web browser.

This is the Storybook part, You design some use cases or configurations that are worth to visual test. Storybook is thery powerful, it also allow the designers or non-coders people to stress test it with different input data or scenarios. Just have a look to the Storybook documentation.
You can visual test the component inside other components and with different widths, in this simple scenario I've just added some margin because on Android the UI do not have it and the simple and advanced use case.

Finally, with commands below, you can see your component on the Web Browser and with the smartphone on your hands. You need Expo app installled on your smartphone to read the QRCode and render React Native.

yarn storybook        # web (react-native-web)
yarn storybook:mobile # native (react-native via expo)

All the code is available here https://github.com/grigio/status-components-sandbox

Conclusion

Finally You have a working environment to test or show your components that permits to manage the software development complexity. You just change a line of code to see the results on the Web and on the Smartphone!
Designers, can just change some configurations to see if the new color or sizes break the project.. without interrupt the developers :D

The true: Write once, Run everywhere !

Curriculum

Sort:  

Thank you for your contribution @luigi-tecnologo.
After reviewing your tutorial we suggest the following points listed below:

  • In your next tutorial place the section of the resource: Link resource

  • Code sections are better displayed using the code markup, instead of placing them as screenshots.

  • Your tutorial is quite short for a good tutorial. We recommend you aim for capturing at least 2-3 concepts.

  • In your next tutorial on Storybook with React Native try to bring more innovative features. This subject is already easily found on internet tutorials. For example: Link

Looking forward to your upcoming tutorials.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thanks for the feedback, the tutorial you linked is for Storybook 3, this one is for Storybook 4

Hi @luigi-tecnologo,

Thank you for your information. Good Work!


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you for your review, @portugalcoin! Keep up the good work!

Hi @luigi-tecnologo!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @luigi-tecnologo!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.26
TRX 0.11
JST 0.033
BTC 63746.34
ETH 3048.25
USDT 1.00
SBD 3.97