React Native KeyboardAvoidingView is not working as expected - react-native

The screen is Wrapped inside KeyboardAvoidingView and ScrollView components. Some of the last inputs are partially hidden by the keyboard. If I set the behaviour prop to anything, but undefined a white element comes up after keyboard. All I would like to do is to make the screen scroll down like 20 more pixels... I feel like I have tried everything so far, even setting the keyboardVerticalOffset prop, but nothing seems to work. If that offset was transparent it would be perfect...
In the following code a View component is passed to children prop
import { FunctionComponent, ReactNode } from 'react';
import { KeyboardAvoidingView, ScrollView } from 'react-native';
interface WrapperProps {
children: ReactNode;
}
const KeyboardAvoidingWrapper: FunctionComponent<WrapperProps> = ({children}) => {
return (
<KeyboardAvoidingView behavior='height' keyboardVerticalOffset={100}>
<ScrollView>
{children}
</ScrollView>
</KeyboardAvoidingView>
);
};

Instead, You can use react-native-keyboard-aware-scroll-view.

I solved my issue with: react-native-avoid-softinput
INSTALLATION:
yarn add react-native-avoid-softinput
On iOS additionally run: npx pod-install
USAGE:
import React from "react";
import { AvoidSoftInputView } from "react-native-avoid-softinput";
const MyComponent = () => {
return (
<AvoidSoftInputView>
{/** Content that should be pushed above the keyboard */}
</AvoidSoftInputView>
);
};
TROUBLESHOOTING:
requireNativeComponent: "AvoidSoftInputView" was not found in the UIManager.
Solution: https://stackoverflow.com/a/65513391/14056591
Try again cd ios && pod install
If it doesn't help, try restarting the simulator, delete and rebuild the app. Restarting Xcode and metro bundler is also a good idea.
If this does not solve your issue, I came across another promising library, but I did not get to use it: react-native-keyboard-controller

Related

BarStyle "dark-content" in StatusBar is not working in IOS (React Native)

I want that barStyle is always be "dark-content" in StatusBar in IOS. However, I see that text is still switching to white, when phone is in dark mode. Is there any solution to this problem? React Native version is "0.63.4".
import {
Platform,
StatusBar,
} from 'react-native';
<StatusBar
barStyle={Platform.OS === 'ios'? 'dark-content': 'default'}
/>
I found a solution for this problem. I removed basStyle from the component and wrote instead in App.js inside useEffect() this code and it started working:
useEffect(() => {
StatusBar.setBarStyle(Platform.OS === 'ios'? 'dark-content': 'default')
}, []);

Icons not showing in my Expo-based React Native App

I have the following code:
import React, { useEffect, useState } from 'react';
import { View } from 'react-native';
import { FontAwesome } from '#expo/vector-icons';
import styles from './styles';
export default function LiveCalls( { navigation }) {
return (
<View>
<FontAwesome name="search" size={204} color="black" />
</View>
);
}
My app runs okay. I'm not getting an errors or warnings. But – no icons are showing. Not even a mysterious "X", where the icon should be. Just nothing. Empty space. Nada. Zilch.
I did check the #expo folder in my node_modules directory to make sure that it's in there... and it is.
I also double checked my icon name, to make sure there was an actual icon in the icon set that I was using.
Additionally, I've tried changing sizes (thinking it might just be super tiny), colors (thinking it might be defaulting to transparent or something), and nothing seems to work.
Any idea what the problem is?

Dark Mode not working React Navigation React Native

I Am working on this to implement Dark Mode in React Native using React Navigation. but it changes only the bottom bar navigator not the screens inside that. can you help me with this
Snack Code
https://snack.expo.io/#belgin/news-app
You're responsible for styling inside your own components. You're styling background as light, setting navigation theme to dark is not gonna magically change the colors you have defined.
For changing themes to work for your components, you need to use the useTheme hook to set colors in your own components instead of hardcoding them.
import * as React from 'react';
import { TouchableOpacity, Text } from 'react-native';
import { useTheme } from '#react-navigation/native';
function MyScreen() {
const { colors } = useTheme();
return (
<View style={{ flex: 1, backgroundColor: colors.background }}>
{/* screen content */}
</View>
);
}
https://reactnavigation.org/docs/themes/#using-the-current-theme-in-your-own-components
Other method is that, you can also create a state which can store your current view-mode (light/dark mode). This is very simple to implement using react-redux. You can refer this video to get better understanding of react and redux.
This is far more simpler implementation of redux.
Note - dependencies such as thunk, react-redux, etc etc are not installed in this video. You can identify which dependencies you're gonna need step-by-step by following error that came in your way. Eg. if createStore gives error try to import createStore as legacy_createstore like done in this question

How to debug a ReactNative app which is not opening

I've been moved to a RN project and I'm new to this. If I create a new app/project is starts perfectly on my phone but this one doesn't. It shows "the app has stopped working".
I tried with a simple index removing the rest of the functions but nothing.
How could I see some logs or where could I remove some other configurations/functions?
index.android.js:
"use strict";
import { AppRegistry } from "react-native";
import App from "./src/index";
AppRegistry.registerComponent("ReactNativeTS", () => App);
/src/index.tsx (the default one):
import React, { Component } from "react";
import { Platform, StyleSheet, Text, View } from "react-native";
const instructions = Platform.select({
ios: "Press Cmd+R to reload,\n" + "Cmd+D or shake for dev menu",
android:
"Double tap R on your keyboard to reload,\n" +
"Shake or press menu button for dev menu"
});
interface Props {}
export default class dejavu extends React.Component<any, any> {
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>Welcome to React Native2rr!</Text>
<Text style={styles.instructions}>To get started, edit App.tsx</Text>
<Text style={styles.instructions}>{instructions}</Text>
</View>
);
}
}
The app has stopped working probably means you're running on Android.
If you have Android Studio, you should use Logcat to see what the exception is.
You'll see the full stack trace and can go from there.
You're probably missing some permissions. Try to add this line to your AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
You'll find this file in android/app/src/main.
react-native log-android will display some information about these errors

StatusBar backgroundColor removed after pressing back button (android)

I'm using the StatusBar component in react native (Android). Here is an example code from my App.js component:
import React, { Component } from 'react';
import { View, StatusBar } from 'react-native';
import { RootNavigator } from './components/Router';
export default class MainApp extends Component {
render() {
return (
<View style={{flex: 1}}>
<StatusBar backgroundColor='black' barStyle="light-content"/>
<RootNavigator />
</View>
);
}
}
The StatusBar is working properly when you launch the app, when you navigate through the entire app and when put in background and then return.
It's NOT working when exiting the app by pressing back button. When you launch the app again, the statusbar backgroundColor is suddenly grey (default color).
Is this a known bug or something? I can't figure out how to fix this.
Alright, shortly after submitting the question I found out about another strategy, using imperative API. I avoided it at first since according to official documentation:
For cases where using a component is not ideal, there
is also an imperative API exposed as static functions on the
component. It is however not recommended to use the static API and the
component for the same prop because any value set by the static API
will get overriden by the one set by the component in the next render.
Here is my revised code:
import React, { Component } from 'react';
import { View, StatusBar } from 'react-native';
import { RootNavigator } from './components/Router';
export default class MainApp extends Component {
componentWillMount() {
StatusBar.setBackgroundColor('black');
}
render() {
return (
<View style={{flex: 1}}>
<StatusBar backgroundColor='black' barStyle="light-content"/>
<RootNavigator />
</View>
);
}
}
It seems like this works properly now. When I press the back button and launch the app again the statusbar remains black. I won't declare this as the correct answer just yet in case someone has an explanation why this happens or a better solution.
Edit: This also appears to work only 90% of the time or so. I've noticed, once in a while, when pressing back button and returning the statusbar remained grey. It is absolutely boggling at this point, I suppose componentWillMount isn't always triggered?
Edit2: After switching to componentDidMount instead of componentWillMount as suggested, it seems to be working 100% of the time now.