When we are developing a good product on any digital platform, following certain features and documentation is crucial to improve our app productivity. In this article, We will explain the basic concepts of Storybook and how to implement it in a React Native app, highlighting the usefulness of Storybook React Native integration for proper documentation.
Basically storybook is a UI design and development tool. It makes the developer’s life easy through faster development and isolating components. In Storybook we have to work on one component at a time. Storybook does not contain our app or product business logic like login, Navigation, etc because the storybook focuses on UI development and testing.
We have to make a separate app for the storybook instead of the original app because it does not contain the navigation and business logic of our product. Storybook helps us to create better documentation for apps. We can test the app with various design-related scenarios and fix the bugs. When multiple people are working on the same app at that time storybook is a more useful feature.
The story captures the component’s rendered state of UI development. We are writing stories based on component-driven development. The story contains multiple substories as per the scenarios. The story file format must be from component name.stories.js | ts | jsx | tsx | mdx. One thing we have to keep in mind is that the storybook file is only for development, not for production or release.
The basic Storybook contains the core features. But Addons gives extra features with the storybook’s core features. Which can be very helpful for development and testing. Addons have APIs so by using them we can customize and configure storybooks in a more productive way. Basic examples of Addons are Knobs and Actions. We will explain both in this article later.
We will use the @storybook/react-native library package to use the storybook in react native. Using this library we can implement a storybook in 2 ways, so first is Using cli, and the second is manual setup. So we will go with the cli method. Hit the below command within the root of your React native app.
npx -p @storybook/cli sb init --type react_native
When you will hit the command you will be asked to install a storybook server, but we will not install it as of now and let it continue. So cli will do a little template creation. So it creates a storybook folder and it contains basic examples and structure with configuration. Screenshots are attached below.
Now in the entry point of our application like App.js or index.js we have to set the below line to configure the storybook in react native.
export {default} from "./storybook";
The Storybook folder has an index.js file that contains the configuration related to all stories as given below.
import { AppRegistry } from 'react-native'; import { getStorybookUI, configure, addDecorator } from '@storybook/react-native'; import { withKnobs } from '@storybook/addon-knobs'; import './rn-addons'; addDecorator(withKnobs); configure(() => { require('./stories'); }, module); const StorybookUIRoot = getStorybookUI({}); AppRegistry.registerComponent('%APP_NAME%', () => StorybookUIRoot); export default StorybookUIRoot;
getStorybookUI method will render the Ui of the storybook in Android and iOS apps. We can also do some configuration in that also. Basically, it contains UI with 3 default tabs Navigator, Preview, And Addons. The screenshot is attached below.
The decorator is used to wrap a story in extra rendering functionality. We can reuse the decorator for multiple stories and substories. We can pass mock data in the decorator and access data in the stories for better development and testing. We will see the use of a decorator when we create a story.
Knobs are a very useful resource because using them developers can play with the story interactively without any kind of changes in code and test the story in dynamic ways. If we use cli method to install the storybook knobs will be installed automatically.
In the stories folder, we have to create one folder named RNText. In that, we have to create 2 files one for the story and one for the UI named RNText.stories.js and index.js respectively. You can add the below data in index.js like the given below.
import React from 'react'; import { View, Text } from 'react-native'; import PropTypes from 'prop-types'; export default function RNText({ title }) { return ( <Text style={{ color: "black", fontSize: 16 }}>{title}</Text> ) } RNText.defaultProps = { title: 'React Native', }; RNText.propTypes = { title: PropTypes.string, };
Now we will write a story in the RNText.stories.js file. To access the story in Storybook we have to import and add the file in the storybook folder’s index.js file where other stories are imported as given below.
import './Button/Button.stories'; import './Welcome/Welcome.stories'; import './RNText/RNText.stories';
Now we will define a story name like “RNText” using storiesOf method like given below.
import React from 'react'; import { storiesOf } from "@storybook/react-native"; storiesOf("RNText", module)
Now we will add a decorator because we have to wrap our story in one component like given below.
import React from 'react'; import { storiesOf } from "@storybook/react-native"; import CenterView from "../CenterView"; storiesOf("RNText", module) .addDecorator((getStory) => (<CenterView>{getStory()}</CenterView>))
CenterView.js will look like this as given below.
import React from 'react'; import PropTypes from 'prop-types'; import { View } from 'react-native'; import style from './style'; export default function CenterView({ children }) { return <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }}>{children}</View>; } CenterView.defaultProps = { children: null, }; CenterView.propTypes = { children: PropTypes.node, };
Now we will add our substory named “technology” and we have to import the RNText component from index.js which we already defined previously.
import React from 'react'; import { text } from "@storybook/addon-knobs"; import { storiesOf } from "@storybook/react-native"; import RNText from "."; import CenterView from "../CenterView"; storiesOf("RNText", module) .addDecorator((getStory) => (<CenterView>{getStory()}</CenterView>)) .add('technology', () => ( <RNText title={text("Title Text", "Google")} /> ));
Here we have to pass the title prop to RNText and we will use knobs here. So we have imported text knobs from addon-knobs lib. It takes the key and default value for that prop title.
So on screen when the RNText component will render we can see Google value displayed. Using knobs we can change the value and see the new updated value on the screen given below in screenshots.
There are different types of Knob we can use in storybook
Now we will install lib for the storybook server. Using a server we can test the storybook component multiple at a time. So we can compare and test on the device at the same time. you can install lib by the below command.
yarn add @storybook/react-native-server
We have to add one script in the package.json file to start a server like
"storybook": "start-storybook"
To start a server we have to hit a command yarn storybook and your server will be started on the browser. After starting a server you have to reload your app once so your application will connect to the server. You can find all the stories in the server which you see in the NAVIGATOR tab on the mobile app screen.
To test an app on a real device you can bind your server with your IP address and the same IP address you have to set as host in the getStorybookUI method given below.
"storybook": "start-storybook -p 7008 -h 192.168.1.3"
const StorybookUIRoot = getStorybookUI({ onDeviceUI: false, port: 7008, host: '192.168.1.3' });
After restarting the server you can test your app using a storybook on multiple devices at the same time.
You can find a short demonstration of the storybook server with a mobile app in given below link: Here
So here I have explained all the basic concepts of Storybook and how you can integrate it with the React Native platform. Storybook React Native is a very interesting tool for UI testing and development.
In a team when multiple people are working on the same product, at that time we can use Storybook React Native for code documentation purposes also. This makes developer and designer life easy and makes our product more interesting.
If you’re looking for a skilled React Native developer to build your app, don’t hesitate to reach out to our team. We have experienced developers who can help you bring your ideas to life. Contact us today to get started!
Ronak is a React-Native developer with more than 4 years of experience in developing mobile applications for Android and iOS. He is dedicated to his work and passion. He worked in different kinds of application industries that have responsive designs. He also has knowledge of how to make an app with expo cli and react-native web and some basic understanding of react.js.
How to Effectively Hire and Manage a Remote Team of Developers.
Download NowMindbowser played a crucial role in helping us bring everything together into a unified, cohesive product. Their commitment to industry-standard coding practices made an enormous difference, allowing developers to seamlessly transition in and out of the project without any confusion....
CEO, MarketsAI
I'm thrilled to be partnering with Mindbowser on our journey with TravelRite. The collaboration has been exceptional, and I’m truly grateful for the dedication and expertise the team has brought to the development process. Their commitment to our mission is...
Founder & CEO, TravelRite
The Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
Mindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork