This tutorial is out of date and no longer maintained.
React Native provides us with a great range of components to design and build fully native user interfaces. These include buttons, views, lists, progress bars, and more.
We also have form inputs components like TextInput
and Picker
. However, when we use them multiple times across an application we might find ourselves wanting a unified reusable pattern.
Note: Picker
has been deprecated and will be removed from React Native core. It can be imported from @react-native-picker/picker
.
In this article, you will build your own custom form inputs for React Native applications.
To complete this tutorial, you’ll need:
This tutorial was verified with Node v16.4.0, npm
v7.19.0, react
v16.13.1, and react-native
v0.63.4.
First, create a new React Native app by entering the following command in your terminal:
Then, navigate to the new directory:
And start the application for iOS:
Alternatively, for Android:
Note: If you experience any problems, you may need to consult troubleshooting issues for React Native CLI.
This will create a skeleton project for you.
There are multiple benefits to having our own input and select components. For example, they both usually have labels and a similar style throughout the app, so we can group that logic together.
Additionally, we can provide a simpler and common API. Both TextInput
and Picker
use different properties for getting and updating the value: value
vs selectedValue
and onChangeText
vs onValueChange
. Furthermore, every time we create a Picker
component we must create a list of Picker.Item
, which can be avoided if we assume a convention and pass an array of key-values to the component.
In order to solve these issues, let’s build custom AppInput
and AppSelect
components.
First, open the App.js
file with your code editor.
Let’s start by creating a BaseInput
component where we can add common functionality, such as the label
:
This code also establishes a common style
using the StyleSheet
API and a label
using the Text
component.
To ensure this input is reusable, the children
are passed to it and rendered inside it.
Now, you can build an AppInput
component using the BaseInput
component and React Native’s TextInput
. Revisit App.js
and add the following lines of code:
The properties that the AppInput
doesn’t use are passed to the BaseInput
component, along with a TextInput
.
Now, you can build an AppSelect
component using the BaseInput
component and React Native’s Picker
. Revisit App.js
and add the following lines of code:
Both components share the common label
, value
, and onChange
props, making the code simpler and easier to use and understand.
So far the BaseInput
component has a default baseInput
style that we’re applying from the stylesheet, but… What if we want to override it?
React Native lets us pass an array to the style
property on the components, so we can take advantage of that by passing an optional style property.
Let’s do that in BaseInput
:
Given the fact that we’re passing down the properties on the AppInput
and AppSelect
components, now if we pass-in a style
prop it will override the default styles:
Following that technique, we can customize the other parts of the components by passing multiple style props, such as rootStyle
, inputStyle
, etc.
In this article, you built your own custom form inputs for React Native applications. This allowed you to reuse common functionality and apply consistency to your Input
and Select
components.
If you’d like to learn more about React, take a look at our How To Code in React.js series, or check out our React topic page for exercises and programming projects.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!