Step-By-Step Guide To Build A Simple Chat App With React Native
Tags: reactnative, chatapp, mobileapps
Published: 2019-11-28 06:12:37

React Native has made the mobile application development comprehensive and quicker. Brands and businesses can build helpful apps for both Android and iOS by utilizing the same platform. 
 
Today, we are going to build a simple chat application by React Native. 
 
Here’s a detailed guide for you to build a simple chat app with React Native. 
 
##**Step 1: Install Required Tools**
 
Since we are using React Native for successfully developing the cross-platform mobile app, we will need some essential tools already installed in our systems so that we can easily synchronize native platforms and react native.
 
First of all, make sure that that the React Native is installed in your system. Since there are so many resources available online, we are not going to add a sub-guide on how to install React Native in Windows/Mac.
 
Then, based on your choice on Android or iOS, You will have to download Android Studio or Xcode in your system. We can also go with other 3rd party simulators to test the code.
 
When are you building a mobile application, you will need to run the app on a mobile simulator. From our experience, we will use Expo to execute this task efficiently.
 
For advanced development in the subsequent steps, verify that the installed version of Node.js is 10+ before installing Expo.
 
Just type Node --version to identify its version, and make sure it is above 10. 
 
Now, we are going to install expo command-line tools. You can replace npm with yarn in this step if you are more comfortable with Yarn.

![install command-line tools](https://lh3.googleusercontent.com/NKQcrjymkCHtMZ9LK1dK7_cSN7OiZX6xs9YLl5Es_cqH5xss8rfqXY_XaQzW3EQQQlX1jFinW9z9U7iSpjDFoJMilV3iE7jgcqC3S5jHIc5CP4pJlZ8ewPgg-rHPt7Z7GQDPEnwV=w420-h83-no)
  
Then, we need to create a project file for building our simple chat app with React Native. You can write below snippet in the CLI to create a project ‘AwesomeChat.’ 
 
![Create a project file](https://lh3.googleusercontent.com/Z6moRVCGfNllpT6w8Lsksqkj6SDuk60pFJ5fFCi18KF20B2XLgJA9f1AAFit98nGQMboRzWW4TTGnfB-ZMrD6XiIvBKDZ_TqI-KIIz9X_yRInAIFgTgm5QVWugLpe2ifxk3OvOn_-g=w1102-h370-no)
  
##**Step 2: Use Stream Chat**
 
We'll build a simple chat application. As a developer, we must avoid writing an extended line of code for building a chat application from scratch, as it will take a lot of time and increase the size of your source code file.
 
Therefore, it is a smart choice to create a library file and intelligently synchronized with the main application. There are so many relevant libraries available that can help you with your requirements for developing a chat application.
 
We are going to use the Stream Chat library here to leverage attractive and elegant chat UI components in our system.
 
With Stream Chat, you can quickly add chat functionality in your mobile application of any kind. 
 
This is the smartest way to conduct the [**React Native App Development**](https://www.esparkinfo.com/mobile-application-development/react-native.html) process by smartly enable the chatting feature in your mobile application.
 
In this process, we will add a simple chat screen in our mobile application using Stream Chat.
 
Now, open the App.js file in a text editor. Write below code snippet inside the file and save it.  
 
![snippet-1](https://lh3.googleusercontent.com/W2OMyzuR9E7kxg9GvRkG4BA9VNP4GLc9vntAoDW-APcCVB6hp0C1-F-T4v3g6GC2ZWMYO0uKkzLBMP3TC1MQFG1wh1IShTohhTqDVGHhZ27t-X3Bf_nFOcWVIlFTn2r-QHVCwkcV3Q=w1064-h657-no)

![snippet-1-continue](https://lh3.googleusercontent.com/23UU19pMcEeu72Km9jE7I_FJuTLCTnyMAz6C5-wQDGCfRH0rZ6kdJtJBN6fGv0-dke0V8TAY2t5pgvrRBACVKDDwUPy25kA7KePakqsFp1AzN7nYTXwPDdzcRVbl_5sovyLREu3gmg=w490-h285-no)
 
Please note that this code snippet is the foundation behind the smooth working of our chat application built from React Native. 
 
So, make sure that you are making these changes without any glitches or typos or syntax errors.
 
The above code snippet will handle all the API calls in your mobile chat application and maintain the state of the app along with manipulating all the other child components. 
 
Therefore, the fundamental working of a simple chat application is heavily dependent on the above code snippet.
 
You can see from the above code snippet; we have a chat component that is responsible for managing all the API calls.
 
Any developer or tester should know where they are going invalid [**developing mobile applications**](https://www.esparkinfo.com/mobile-application-development.html). An efficient way to determine the development cycle is to run the mobile app on the smartphone.
 
As soon as you run the application on the smartphone, you will get certain types of screens and layouts that will help you identify your code is behaving.
 
In our case, to get the preview of the screen that we have written below, we are going to install Expo on our smartphone. 
 
After installing Expo in your phone, make sure that the smartphone is connected with the same Wi-Fi network where your computer system is connected with.
 
It might happen that you are using the Android Studio or Xcode to develop the application. In that case, you can also utilize your inbuilt simulator to evaluate the performance of your code.
 
![npm-start](https://lh3.googleusercontent.com/mnNteNgAt_a_vRuqaphzoxkRjuVcFpAo2L1fXFxZZ7l1Q_43Fdwp7eSp_7cIw3B5Ard0LHN4WdEzKGHG4l8PUwdvbxI08ZcLS_G3skW_sSOs57dL43v5qba0TF79Np8ei5zIXLyJnQ=w477-h139-no)
 
After writing the above line, the React Native development server will be initiated. Please note that the server will keep your application changing as soon as you make any changes in your code snippet.  
 
##**Step 3: Manage Multiple Chats on One Screen**
 
The feasibility of any application or software is determined by the way it can handle multiple tasks in a given moment. The more task it can execute, the better the app is.
 
Similarly, the chat application must be able to manage multiple conversations in a given moment and provide efficient notifications as receiving multiple messages from multiple contacts.
 
In the single application, you will be able to talk with multiple users simultaneously.
You cannot run the application again or from the other device to chat with another user. 
 
Just like Facebook messenger, Hike, Telegram, WhatsApp, and most of the other chat applications, you must be able to talk to several contacts simultaneously within a single login.
 
Therefore, we need a screen where we can have a list of conversations with whom you are talking to.
 
Moreover, we will also need robust navigation facilities in our chat application to effortlessly roam around the mobile app and have a seamless chatting experience with our contacts.
 
The package which we have installed earlier can execute all the tasks efficiently and help us enable multiple chats efficiently.
 
So, we would again go to the App.js file and make the following changes. 
 
![snippet-2](https://lh3.googleusercontent.com/HOB1GTh1oPLrBmTs8SQqph8WZ8RleWuYUaDOSZs5v74Xhpl8iIq4YU6YIhXucOHB9XrXhFx2hKG_Y7YrmYAJTT3IF6CCjT2wkRB_UVeJfiN8NrutE44--Ktc97uQ4ZsWFztVN10XQQ=w1137-h657-no)
 
![snippet-2-1](https://lh3.googleusercontent.com/7D2liWiqggaplT2sYKrEcxug_22BZLuAOD0gxfpruOs37qHnvnjScxQQvH7T0Ra_KZLP6vjgcY5XgT4a8CmYV6nwUgibPWfy5ocnJRkWyK-3IeM9eSqiQSDIH8pnHYV15eASHq73xg=w766-h657-no)

![snippet-2-2](https://lh3.googleusercontent.com/AxrWkYSTXFBaWeXzJtcNjDdKLlu4mv0i8Fdewvy1D0a73FD_w9QMZ2ZqPjcUkatV8NRj-0q6FBrt3kJBHXumI16TtAFkp-KrR3pd2GFB4bXe3Cznyg4_ODj7gRa19L7HKrP8Fz2nQQ=w820-h648-no)

After making changes in the App.js file and saving it, you can go back to the simulator or the application on your mobile phones to see the changes.
 
Now, you will get a dedicated screen with a list of conversations. You can tap on each of the conversations to evaluate the things we have discussed above.
 
On all leading chatting applications, you will observe that a particular conversation comes at the top of the list as soon as it gets a new message. 
 
In case of multiple your messages, the list is sorted by brining the conversation with the most recent messages at the top of the list.
 
Technically, each conversation is considered as an independent channel, and the conversation list is updated as soon as there is a new message from a channel.
 
Tool deal with all these States, we have a component called ChannelList that handles custom query and ordering. 
 
Remember messaging someone for the first time, and their name is automatically added to the list? This is also done by ChannelList as it works as pagination and updates the conversation list as soon as a new channel is created. 
 
##**Step 4: Add Channel Preview**
 
When you open chat application after some time, you would have more than one message from a single conversation. 
 
Since we have already covered the list of the conversation in the above step, now we have to enable preview count to show how many messages have come has from a particular conversation. 
 
To enable this, you will have to do a lot more manual coding on your native programming languages. 
 
Since you are using React Native, you can manipulate code effortlessly and achieve this feature without doing a lot of manual programming. 
 
![Snippet-3-1](https://lh3.googleusercontent.com/e2OMUVa84w8BaZ0-N9J3PnQoNKoDF2rw2R28n7YQxBseeak8t4qVhNw2IOyL7UXYtjTUC3puOb9p2uUKHlxJ5UI158XbAF_xWnr__J__G8N-Qn8wrj-gmVio9qhCG0SCgx7nof7mFw=w1044-h657-no)
![Snippet-3-2](https://lh3.googleusercontent.com/8uyd0q0o9hZH8HjAzvi6AmrweIJGvXKMig4tFWwVWj63GUAsRjUlVfQBc717kFYfOi5ofP1YqsfnKbwG-IpvLr7VRwVUjJSVIOg4bzWv2kG4egaFY0OB7sQyL3MnR8opAN_NVUI-9g=w1044-h657-no)
![Snippet-3-3](https://lh3.googleusercontent.com/nFCHJNHNXxyZW_WRhP_pomeqWPlZmFcCAeGmntH4UWoWbYmVqQNK_DI7imGwLyBdOsr5dgzBkxEsFxpOTmS_68QVlkJOpaocL9IwKPOmeynWpKraznxu7dcoVGIaWWetiEOMRRoy8w=w1044-h657-no)
![Snippet-3-4](https://lh3.googleusercontent.com/VGX23cDoG0-F4Xo24J4pZmlSgKymeXG6CtNlv-v9veHULG5WGP47E9CpxAmeUbMznCIo8tqLC81k3DLaTsfdiUkp4DqriOqq8cWaPHIhrKUYegnapHuqdEyWvXNxj7L2M1M-9RQfQQ=w1044-h657-no)
![Snippet-3-5](https://lh3.googleusercontent.com/qF7vxtcsLnwJ7Ip7JfqchQGiK5ZwqfAUA4WvEAeJLKESweI4NJblikGY7cOzxeOBrKmzE21kwDV9bkzc0Ct15jiGfyiq77Ll-Uw_UQHi_SYbN6020X6l3caCbPYqb4BkC5NLXxJEOA=w1118-h596-no)
 
##**Step 5: Create Message Threads** 
 
Stream Chat supports multiple Threads under the same conversation to easily bifurcate your messages as per your comfort.
 
In technical language, you will be able to generate sub-conversations from a single channel without initiating different communication modes.
 
Stream Chat will enable thread component React Navigation in our mobile application to achieve this functionality.
 
In the code below, we will create the ThreadScreen component. Then register the new chat screen to navigation. 

For knowing more about it, you should contact & [**Hire React Native Developer**](https://www.esparkinfo.com/hire-dedicated-developer/react-native.html) from a reputed firm. He/She will be able to guide you on this subject.
 
We would be leveraging the onThreadSelect prop to navigate to ThreadScreen.   

![Snippet-4-1](https://lh3.googleusercontent.com/xffsDt8Ntl3uglJnqOIyHEg-1Q4Hn4Somh_UFI0DlIeLdJmeE4AnS8B4RUbhgwC4FPlZW2Lw5J_DXInE27Qitc5WmKv24fJ7NE0wKZsEDrREG1F0O-dk9XURdBOJpRPMjHjlQNpw5Q=w1067-h657-no)
![Snippet-4-2](https://lh3.googleusercontent.com/gz20yRiQ55M7JimXPqSs9SaumzMzKGTCPp-YSuf_ujxVGA7bVDlanZbvP6_JMi_Kvi5dC32uINqoc7lbXbpSOLDmWMxzbUiV83c85YKWkKvtZjgUoj5gcmWM4LUXMJKtW5L-zS1H6A=w1067-h657-no)
![Snippet-4-3](https://lh3.googleusercontent.com/zphTWFuWNDxCqJu08G4cdSuEJk9hS0TqbowTuikDiX-kNZtqDEJqZEEkfJSYcnvPEVQ2JY1nO9QCn-68UCfSdfqfD9017o0clR9Zqe-7UzNqYztFkpkvwMS09N-6yZU_C3g7KXHA2g=w1067-h657-no)
![Snippet-4-4](https://lh3.googleusercontent.com/l9kHsf-x9QB90gbY5kHvKiPcXMSbSHIQja4Ry6VsVRoL37LQOVV4dOnBFDGwhC0oz2RRwhyLWMkaSN9EaNmAyd0ncD54RfGVpt4fZM0vZrkBufh85sNmw_ClAM68zjkCzSDzl6jb0g=w1067-h657-no)
![Snippet-4-5](https://lh3.googleusercontent.com/DZs-cFr0HELlYWyQ1SnF-GW45iwEI3BIVEB0wLagJepJ592KapzN6ceeNJKo94QzkunR-2hlxih7N8Mcl75QSFABiyeC3KLzzxwUzLhPlqS_-jMQzDwYd7Sr_tywFlAkgYXEZpRNcA=w698-h467-no)
 
##**Step 6: Applying Custom Message** 
 
You must also take care of how the messages are rendered in your mobile application. 
 
In the below code snippet, we have made a custom message component that uses the compact layout for messages. 

![Snippet-5-1](https://lh3.googleusercontent.com/UlcbO0YD1DLRs3oKEUPAwHBKSPR16sirj4y3_w3cDideZkqnlKMZhUx5lBWF6biR34WLcnEE0DYgVmPla9XSSOwVCD21utwnRloeef4QmUWoivmbfYqcR43nx6Ii1hCtWxBtn7JanQ=w1072-h657-no)
![Snippet-5-2](https://lh3.googleusercontent.com/zHXByOEnsb7UxtObenQ-5et6xLSTmbKkHVTKTeWOsZ6hw00GYoqmMwzIxxFGgFlElHGezsofFjiUe-Y6gzthStWkdkpvEpO2kKG8HL-XVzBdOm3b7NdFcCVvWLk4TcYrh4LpbA5UEg=w770-h579-no)
 
##**Step 7: Integrate Custom Styles** 
 
Custom styling makes your mobile app’s layout more appealing and elegant to use. We are going to leverage the React Native platform for this without writing extended CSS and HTML code. 

In Fact, this is one of the [**Top Reasons Why People Opt For React Native**](https://www.esparkinfo.com/react-vs-angular.html). SO, you should also start preparing for this technology, right now.! 
 
![update-default-color-to-green](https://lh3.googleusercontent.com/QmLRz_AjJhN7bhwpLjI66_t72K5quah6v1AJYVQ2om1CEZ3Xu9ZgBAuhYdD4jLuwibapkdoH-TcqLnG9FfMC8nh-dlthphavt3zs_LKVZCaFTPurN7GwQ0FLoLQyZVyL_JTe-UWJcQ=w294-h211-no)
 
This code will update the default color to green. 
 
##**Conclusion**
 
I’m curious to know how do you find this detailed guide of creating a chat app with React Native. Please drop your comments below and let me know. 
 
If you are using any other method or have added some additional features with the similar methods discussed above, don’t forget to mention your approach below to help other programmers grow.