{"id":3985,"date":"2023-11-04T23:13:58","date_gmt":"2023-11-04T23:13:58","guid":{"rendered":"http:\/\/localhost:10003\/building-web-applications-with-react-native\/"},"modified":"2023-11-05T05:48:25","modified_gmt":"2023-11-05T05:48:25","slug":"building-web-applications-with-react-native","status":"publish","type":"post","link":"http:\/\/localhost:10003\/building-web-applications-with-react-native\/","title":{"rendered":"Building web applications with React Native"},"content":{"rendered":"
React Native is a JavaScript library that allows developers to build mobile applications for iOS and Android platforms using the same code base. It is based on the popular React web framework developed by Facebook. But did you know that it is also possible to build web applications using React Native? In this tutorial, we will explore how to use React Native for web development.<\/p>\n
Before we begin, ensure that you have the following requirements installed in your system:<\/p>\n
To create a new React Native project, open your terminal and execute the following command:<\/p>\n
npx react-native init my-app\n<\/code><\/pre>\nThis command creates a new project named my-app<\/code> in your current working directory. Once the process is complete, navigate to the project directory and execute the command below to start the development server:<\/p>\ncd my-app\nnpm start\n<\/code><\/pre>\nThis opens the Metro Bundler<\/strong> console in your terminal. Metro is a JavaScript bundler responsible for building your application. In addition to the bundler console, it opens a browser tab where you can run your application in development mode.<\/p>\nConfiguring React Native for Web<\/h2>\n
In order to use React Native for web development, you will need to install the react-native-web<\/strong> package. This package provides a way to render React Native components in the browser using CSS and HTML.<\/p>\nTo install it, execute the following command:<\/p>\n
npm install react-native-web\n<\/code><\/pre>\nAfter installing the package, we need to modify the project configuration to support web development. In the project root directory, create a file named metro.config.js<\/code> and include the following modifications:<\/p>\nconst { getDefaultConfig } = require('metro-config');\n\nconst config = getDefaultConfig(__dirname);\n\nmodule.exports = {\n ...config,\n resolver: {\n ...config.resolver,\n sourceExts: [...config.resolver.sourceExts, \"web.js\", \"web.ts\", \"web.tsx\"],\n },\n};\n<\/code><\/pre>\nThis configuration maps all the .web.js<\/code>, .web.ts<\/code>, and .web.tsx<\/code> files to be recognized by the bundler. We will use these files to write web-specific code.<\/p>\nCreating a Shared Component<\/h2>\n
We will start by creating a shared component that we will use in both platforms. In the src<\/code> directory, create a file named Button.js<\/code>. This file defines a simple button component that accepts the title<\/code> and onPress<\/code> props:<\/p>\nimport React from \"react\";\nimport { TouchableOpacity } from \"react-native\";\n\nconst Button = ({ title, onPress }) => (\n <TouchableOpacity onPress={onPress} style={{ backgroundColor: '#6495ED', padding: 10 }}>\n <Text>{title}<\/Text>\n <\/TouchableOpacity>\n);\n\nexport default Button;\n<\/code><\/pre>\nThis button component will have the same appearance in both the web and mobile platforms. To use this component in the web platform, we will create a duplicate file named Button.web.js<\/code> that renders the same button using HTML and CSS:<\/p>\nimport React from \"react\";\n\nconst Button = ({ title, onPress }) => (\n <button onClick={onPress} style={{ backgroundColor: '#6495ED', padding: 10 }}>\n {title}\n <\/button>\n);\n\nexport default Button;\n<\/code><\/pre>\nNotice that we changed the TouchableOpacity<\/code> component to a button HTML element. Also, we removed the import { TouchableOpacity } from \"react-native\";<\/code> line.<\/p>\nCreating the Web Platform<\/h2>\n
With the shared component ready, let’s create the web platform by adding a new file named index.web.js<\/code> in the project root directory. This file will contain the web application entry point.<\/p>\nimport { AppRegistry } from \"react-native\";\nimport App from \".\/src\/App\";\nimport { name as appName } from \".\/app.json\";\n\nAppRegistry.registerComponent(appName, () => App);\n\nAppRegistry.runApplication(appName, {\n initialProps: {},\n rootTag: document.getElementById(\"root\"),\n});\n<\/code><\/pre>\nThis code registers the main App<\/code> component and runs it on the #root<\/code> element in the index.html<\/code> file. In order to use React Native components in the web platform, we need to modify the index.html<\/code> file to include the necessary script tags. In the project root directory, create a file named index.html<\/code> with the following content:<\/p>\n<!DOCTYPE html>\n<html>\n <head>\n <title>React Native Web App<\/title>\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" \/>\n <script src=\"https:\/\/unpkg.com\/react-native-web\/dist\/index.js\"><\/script>\n <script src=\".\/index.web.js\"><\/script>\n <\/head>\n <body>\n <div id=\"root\"><\/div>\n <\/body>\n<\/html>\n<\/code><\/pre>\nThis file includes the react-native-web<\/code> and index.web.js<\/code> script tags. It also includes the #root<\/code> element where we will render our application.<\/p>\nBuilding for Web<\/h2>\n
We are now ready to build and run our application. Execute the following command in your terminal:<\/p>\n
npm run web\n<\/code><\/pre>\nThis command starts the webpack<\/strong> bundler and generates a dist<\/code> directory with the built application files. Once the process is complete, open the index.html<\/code> file in your web browser to see your React Native web application running.<\/p>\nCreating the Mobile Platform<\/h2>\n
To create the mobile platform, we will reuse the shared component defined earlier. In the src<\/code> directory, create a file named App.js<\/code> and include the following code:<\/p>\nimport React from \"react\";\nimport { StyleSheet, View } from \"react-native\";\nimport Button from \".\/Button\";\n\nconst App = () => {\n return (\n <View style={styles.container}>\n <Button title=\"Press Me!\" onPress={() => alert(\"Hello World!\")} \/>\n <\/View>\n );\n};\n\nconst styles = StyleSheet.create({\n container: {\n flex: 1,\n alignItems: \"center\",\n justifyContent: \"center\",\n },\n});\n\nexport default App;\n<\/code><\/pre>\nThis file defines the main App<\/code> component that renders the shared Button<\/code> component. We also defined a StyleSheet<\/code> object to style the container.<\/p>\nBuilding for Mobile<\/h2>\n
To build the mobile application, execute the following command in your terminal:<\/p>\n
npx react-native run-android\n<\/code><\/pre>\nThis command launches the application on an Android emulator or device connected to your computer. You can also use the run-ios<\/code> command to launch the application on an iOS simulator or device.<\/p>\nConclusion<\/h2>\n
In this tutorial, we explored how to use React Native for web development. We started by configuring React Native for web development, created a shared button component, and built both a web and a mobile platform using the same code base. We hope this tutorial will help you get started with building web applications using React Native. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"
React Native is a JavaScript library that allows developers to build mobile applications for iOS and Android platforms using the same code base. It is based on the popular React web framework developed by Facebook. But did you know that it is also possible to build web applications using React Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[605,698],"yoast_head":"\nBuilding web applications with React Native - Pantherax Blogs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n