How to properly implement the statusbar in the components using React Native - reactjs

I have task where I need to change the statusbar from white and make the content dark. Now I search how the statusbar implement to the component. Yes it works properly. Later on I experience bugs where If I re render the entire application I mean when I re open the application again. the status bar that I change it turns to the original color which is blue. so to make more details I will show you the code that I implement and the screenshot of my application that I created.
Here is some research that I read:
Reference
I don't think there is any conflict between react-navigation and the StatusBar, but I think you should use the component rather than the imperative API. There's a high chance this is due to a re-render of your app and it just switch back to the default, with a component declare, you ensure it won't happen.
Goal: To make the statusbar won't change to blue if I re open the application again.
Here is what I implement on my application:
React Navigation 5 (For Navigation)
Native Base
Statusbar:
<StatusBar hidden={false} animated barStyle="dark-content" backgroundColor="#FFFF" />
Bug Status Bar:
Goal Status Bar:

Code Works might only for the android device as we have to manage the safeAreaView in iOS device
Check out the below code example to show the custom status bar color.
<View style={{ flex: 1 }} >
<StatusBar animated backgroundColor="pink" barStyle="dark-content" />
<View style={{ flex: 1, backgroundColor: "green" }} >
</View>
</View>
Might you will be able to see the status bar color pink and dark content into it and whole screen with green color.

Related

Can I add an animation to a React Native navigation button?

I have a button in react native, that I am using to navigate to another screen in my Drawer navigator (in addition to using the drawer screen itself). However, the transition is very abrupt. It it possible to have some kind of animation to make it smoother?
I have seen the docs for the transitioner, but these do not appear to be for a functional component like the screen the button is on. Is there any help?
Here is my button:
<TouchableOpacity
style={styles.buttonContainer}
onPress={() => this.props.navigation.navigate("SocialScreen")}>
<Text style={styles.buttonText}>Socials</Text>
</TouchableOpacity>
Any help is appreciated!
Of course you can add animation there.
I am attaching link of library with detail explanation react-native-navigation
Kindly comment if you face any error.

React Native Notification Tray and AppBar color

I am using react-native-paper along with react-navigation and the notification tray background color is not a darker shade of the AppBar color as shown in the documentations of react-native-paper and I cannot figure a way out to do that.
I have a basic project implemented and the snack of it can be seen in the following link:
https://snack.expo.dev/#throwawayacc/grumpy-milkshake
Expected Output:
Current Output
As it can be seen above the notification tray color is the same as the AppBar color. How do I get it to work like the expected output. You can see the current implementation on the link above.
You can use this https://reactnative.dev/docs/statusbar#backgroundcolor-android to change the status bar to a different color.
Just import the StatusBar from react-native. And put this code in your screens components like this. e.g.
// codes...
function HomeScreen(props) {
return (
<View style={style.container}>
<StatusBar
animated={true}
backgroundColor="#3B008F"
/>
<Text>Home Screen</Text>
// codes...

React Native ScrollView needs very violent scrolling to scroll

On Android (and possibly iOS, I haven't tested), there is a problem with scrolling text with a React Native Scrollview. I am attempting to create a text block component that flexes to fill the remaining space of the page with text rendered inside of it. That works.
My problem is that this text needs to be easily scrollable. In my application, I can not make minor scrolling adjustments to the text in this field. (Think about scrolling on Instagram, you can scroll up or down in very small gestures).
This text in the scrollview does not scroll unless you give it a very long and quick flick of the finger. It does scroll and it scrolls correctly functionality-wise, the user experience is just awful.
It's as if it doesn't get recognized as a scroll view unless you scroll it a massive amount.
This happens with normal text elements getting passed into props.children. I am just setting fontSize on the text elements so that the text is bigger. Right now the text font size is set to 60 so that I can test the scrolling functionality.
Here is my text block code:
const TextBlock = (props) => {
return (
<View style={{...props.style, ...styles.container}}>
<ScrollView style={styles.scrollView} contentContainerStyle={{flexGrow: 1}}>
{props.children}
</ScrollView>
</View>
);
};
const styles = StyleSheet.create({
container: {
backgroundColor: Colors.whiteBackgroundColor,
paddingHorizontal: 18,
paddingVertical: 10,
borderRadius: 20,
width: '90%',
flex: 1
}
});
Any help on this would be greatly appreciated. I've tried putting a flex grow on the inner elements. I have tried wrapping the inside text in another view (both with and without flex attributes). I have tried removing the flex altogether.
Please let me know if there's some way to fix this.

React native: how to generate blank whitespace that has the same size as a component which will later be rendered?

This is from a Udacity flashcard project. Say I have this screen:
A "Next card" button appears conditionally upon the user hitting "Correct" or "Incorrect" (a value gets set in component state and the button is rendered conditional on that value being true):
All of this content is inside a <View> with alignItems: 'center' and justifyContent: 'center', so when the button appears, everything shifts slightly along the vertical axis.
I'd like to create some kind of "blank" placeholder that will have the exact same size as the "Next card" button and have it "render" before the Correct/Incorrect button is hit, so that this shift does not occur.
I recognize I could set a hard height/width on the button and make some kind of placeholder item with the same size, but this is a general question and I'd like to leave appearance to device defaults as much as possible.
Is this possible, and/or a sensible approach? If not, why not, and what would you suggest instead?
Thank you!
I assume you're doing something like this to render the Next Card button.
{this.state.answerSubmitted && (
<TouchableOpacity>
<Text>Next Card</Text>
</TouchableOpacity>
)}
Instead, you could do something like this, your button will be there, just not visible and clickable.
<TouchableOpacity
disabled={!this.state.answerSubmitted}
style={{opacity: this.state.answerSubmitted ? 1 : 0}}
>
<Text>Next Card</Text>
</TouchableOpacity>
If you already have a style for the button, you can use the array notation.
style={[styles.yourStyle, {opacity: this.state.answerSubmitted ? 1 : 0}]}

How to have linear gradient background on all screens in React Native

I have an app written in React Native. I would like to have a gradient background in all screens. Maybe something like:
I have found the react-native-linear-gradient package, but would I get higher performance using an image?
Also, how do I make sure it stays as a background on all pages?
Would it be something like
const Container = (props) => (
<View>
<LinearGradient colors=['blue', 'orange', 'blue'] />
{props.children}
</View>
);
and then on all my screens use
<Container>
..
</Container>
You can try to create gradient image in some graphical editors which allow you to set linear gradient from top(blue) to bottom(transparent), and place this image as background all your scenes. Its can be helpful.
But if you need to cange colors of your gradient dynamically, you need to use libraries for now.

Resources