How to implement RAM bundle and Inline Requires react native - react-native

I am using react native to build project and I am facing a problem with long launch time, I try to follow https://reactnative.dev/docs/ram-bundles-inline-requires, however it is not so clear about Investigating the Loaded Modules, and how to put only the necessary modules for first screen.
I am not also able to find index.(ios|android).js file (is it index.android.bundle).
If you can tell me how to extract only necessary modules and recommend docs or examples about implementing that?

With considering the official documents, pay attention to this example:
You have a Main screen (view) in your app like HomeScreen
In your HomeScreen, there are more and more components and logic in this page but assume we have a SettingsModal.
usually, modals will open when you touch a button.
Without inline require
you have to import your modal component to your HomeScreen at the top level of your module
with inline require
you will import your modal component to your HomeScreen when it needs to show!
Let's do this with code:
HomeScreen without inline require
import React, {useState} from 'react'
import {View, Text, Pressable} from 'react-native'
import SettingsModal from 'components/modal'
function HomeScreen() {
const [showModal, setShowModal] = useState(false)
const handleShowModal = () => setShowModal(prevState => !prevState)
return (
<View>
<Text> Home Screen </Text>
<Pressable onPress={handleShowModal}>
<Text> show settings </Text>
</Pressable >
{
showModal
? <SettingsModal />
: null
}
</View>
)
}
In the above example, we import SettingsModal in our HomeScreen top level with React and View and Text...
HomeScreen with inline require
import React, {useState} from 'react'
import {View, Text, Pressable} from 'react-native'
let SettingsModal = null;
function HomeScreen() {
const [showModal, setShowModal] = useState(false)
const handleShowModal = prevState => {
if (SettingsModal == null) {
SettingsModal = require('components/modal').SettingsModal
}
setShowModal(prevState => !prevState)
}
return (
<View>
<Text> Home Screen </Text>
<Pressable onPress={handleShowModal}>
<Text> show settings </Text>
</Pressable >
{
showModal
? <SettingsModal />
: null
}
</View>
)
}
In the above example, we check if SettingsModal has not been imported yet, then we will import this component to our HomeScreen file (after user touch the show settings button)

Related

navigation does not work when imported - React native

When I use props.navigation.navigate("example"), it works normally. But if I import the component on another page it doesn't work anymore, props returns an empty object.
Works Fine:
const Menu = props =>{
console.log(props)
return(
<View style={styles.menuStyle}>
<TouchableOpacity style={styles.topicDiv} onPress={() =>props.navigation.navigate("Ads")}>
<View>
<Image style={styles.topicStyle} source={require ("../assets/security-camera.png")}/>
<Text style={styles.textStyle}>Câmeras</Text>
<Text style={styles.subTextStyle}>Veja como está a praia ao vivo 📷</Text>
If i try import Menu, navigation does not work:
import React from "react";
import { View } from "react-native";
import Menu from "./menu";
const Supermenu = () =>{
return(
<View>
<Menu></Menu>
</View>
)
}
export default Supermenu
If you use <Menu> inside of another component like <Supermenu>, React Navigation has no way to pass its navigation property in there. It only happens automatically if a component is a direct child of a screen (or its component property).
To have navigation available in Menu regardless of its position in the hierarchy, as long as it's a child of <NavigationContainer>, the best way is to make use of the useNavigation hook:
import { useNavigation } from '#react-navigation/core';
const Menu = props =>{
const navigation = useNavigation();
return (
<View style={styles.menuStyle}>
<TouchableOpacity style={styles.topicDiv} onPress={() => navigation.navigate("Ads")}>
...
See documentation for more detail.
If you are on an older version, there was also a HOC withNavigation that you could use.
You could also do the same in Supermenu and then pass navigation down manually.
use import {userNavigation} from '#react-navigation/core' instead of props navigate or you can add navigation props to the Menu component.

BarCode Scanning in React-Native

I was using react-native-barcode-scanner-google but its not working now.i have two screen on my home page one of them is barcode scanner screen.when i click on barcode screen i got message "unfortunatly the app has stoped".I also try other barcode scanner libraries but not working.Im new to react native,So any help would be highly appreciated.
Sample code
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Text, View, Alert } from 'react-native';
import BarcodeScanner from 'react-native-barcode-scanner-google';
export default class BarcodeApp extends Component {
render() {
return (
<View style={{flex: 1}}>
<BarcodeScanner
style={{flex: 1}}
onBarcodeRead={({data, type}) => {
// handle your scanned barcodes here!
// as an example, we show an alert:
Alert.alert(`Barcode '${data}' of type '${type}' was scanned.`);
}}
/>
</View>
);
}
}
AppRegistry.registerComponent('BarcodeApp', () => BarcodeApp);

React Native | Auto Width 100%

I am using React Native version 0.63.4. There is a bug that I just noticed and that didn't happen before. It does -width: '100%' - as if I added it myself to the style of any object (Text, View) I created. This was not the case before. For example, I want to paint the background of a text red. But just as much space as it is. Not all. I would add -width: '100%' - if I wanted this. But now it adds this automatically. What is the cause and solution?
import React from 'react';
import {View, Text} from 'react-native';
const App = () => {
return (
<View style={{flex: 1}}>
<Text style={{marginTop: 80, backgroundColor: 'red'}}>EXAMPLE</Text>
</View>
);
};
export default App;
Try this way because It will take full width by default, If you want to wrap then try adding alignSelf:"flex-start" like
import React from 'react';
import {View, Text} from 'react-native';
const App = () => {
return (
...
<Text style={{... , alignSelf:"flex-start"}}>EXAMPLE</Text>
...
);
};
export default App;

React Native + NativeBase +Exponent, Element type is invalid error

I am fairly new to react native, nativebase, and Exponent. Unfortunately, I am having trouble getting simple components even to display. I've spent probably 6+ hours troubleshooting simple tutorial components from the nativebase documentation. I feel like I'm just missing something fundamental and I would really appreciate a helping hand.
Here's what I'm doing:
Using Exponent XDE to run my project.
Installed nativebase according to the documentation, no errors at this point.
main.js
'use strict';
import React, { Component } from 'react';
import {
AppRegistry,
} from 'react-native';
import { Container } from 'native-base';
import { CardImageExample } from './component/card.js';
AppRegistry.registerComponent('main', () => CardImageExample);
card.js
import React, { Component, Image } from 'react';
import { Container, Content, Card, CardItem, Thumbnail, Text, Icon } from 'native-base';
class CardImageExample extends Component {
render() {
return (
<Container>
<Content>
<Card>
<CardItem>
<Thumbnail source={require('./img/penguin-icon.png')} />
<Text>Instrumental Songs</Text>
<Text note>Guitar</Text>
</CardItem>
<CardItem>
<Image style={{ resizeMode: 'cover' }} source={require('./img/penguin.jpg')} />
</CardItem>
<CardItem>
<Icon name='ios-musical-notes' style={{color : '#ED4A6A'}} />
<Text>Listen now</Text>
</CardItem>
</Card>
</Content>
</Container>
);
}
}
export default CardImageExample;
Current error:
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined
I just have absolutely no idea where to start. Thanks.
Your main.js should like some thing Like
import * as Exponent from 'exponent';
import React from 'react';
import First from './src/app';
import { Container, Header, Title, Content, Button, Left, Right, Body,Icon, Separator } from 'native-base';
class App extends React.Component {
render() {
return <First / > ;
}
}
Exponent.registerRootComponent(App);
You have to register your app like this Exponent.registerRootComponent(App).
If you are using Exponent.
Example: https://github.com/exponent/nativebase-example

App Freezes after React Native Navigator is called

i'm working on my first react native app that uses the Navigator and i have encountered an issue. Anytime i press on the <TouchableOpacity /> to make the push to the navigator, the app freezes and can't be press again.
here's my code
```
import React, { Component } from 'react';
import {AppRegistry, Navigator, Text, TouchableOpacity} from 'react-native';
import App from './src/App';
import SinglePost from './src/components/SinglePost';
class AppNavigator extends Component{
renderScene(route, navigator){
var navProps = {navigator};
switch (route.id) {
case "postsList":
console.log(route)
return (
<TouchableOpacity onPress={() => navigator.push({id:'yes'})}>
<Text>Hey</Text>
</TouchableOpacity>
)
case "singlePost":
return <SinglePost title="Post"/>
case "yes":
console.log("yes route ",route)
return <Text>Yes</Text>
default:
return <Text>Yes</Text>
}
}
render(){
return (
<Navigator
initialRoute={{id: "postsList"}}
renderScene={this.renderScene}
/>
)
}
}
AppRegistry.registerComponent('SocialMe', () => AppNavigator);
```
When I click the yes, it just freezes, any explanation for this and how can I resolve this? Thanks
Found the problem. I noticed that this only happens when I'm debugging Js remotely. If I disable it, then all works fine