Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change app background color in React Native

I'm trying to change the color of the background in my react native app, from grey to white. I'm using react-navigation to make a TabNavigator after I render it. I tried to put this TabNavigator in a view and set backgroundColor but all screen became white. How can I solve this?

index.js

... render() {     return (         <View style={{ backgroundColor: '#FFFFFF'}}>             <Tabs />         </View>     )   } ... 

Tabs

... const Tabs = TabNavigator(   {     Home: {       screen: HomeStack,       navigationOptions: {         title: 'Acasa',       },     },     ...     Account: {       screen: AccountScreen,       navigationOptions: {         title: 'Contul meu',       },     },   },   {     tabBarComponent: props => <FooterNavigation {...props} />,     tabBarPosition: 'bottom',     initialRouteName: 'Home',   }, ); ... 

Home Screen

render() {     const {       data, refreshing, loading, error,     } = this.state;      return (       <ScrollView>         <Container>           {error && <Text>Error</Text>}           {loading && <ActivityIndicator animating size="large" />}            <List>             <FlatList               data={data}               renderItem={({ item }) => (                 <ListItem onPress={() => this.props.navigation.navigate('Item', item)}>                   <Item data={item} />                 </ListItem>               )}               // ID from database as a key               keyExtractor={item => item.title}               ItemSeparatorComponent={this.renderSeparator}               ListFooterComponent={this.renderFooter}               ListHeaderComponent={this.renderHeader}               refreshing={refreshing}               onRefresh={this.handleRefresh}               onEndReached={this.handleLoadMore}               onEndReachedThreshold={0}             />           </List>         </Container>       </ScrollView>     );   } 
like image 801
Grigore Budac Avatar asked May 31 '18 08:05

Grigore Budac


People also ask

How do I change the background color in React Native app?

In React Native we can use backgroundColor property of stylesheet to change the screen color to white, black, yellow etc. React Native beginners makes mistake by using background property instead of backgroundColor . This works in React and HTML but not in React native. The hex code for white color is #FFFFFF or #FFF.

How do I change background color in react?

To change background color on click in React:Set the onClick prop on the element. When the element is clicked, set the active state. Use a ternary operator to conditionally set the background color based on the state variable.


2 Answers

I've solved my problem, it was caused by StackNavigator. To solve it , just add some extra options

    const HomeStack = StackNavigator(   {     Home: {       screen: HomeScreen,     },     Item: {       screen: ItemScreen,       navigationOptions: ({ navigation }) => ({         title: `${navigation.state.params.title}`,       }),     },   },   **   {     headerMode: 'screen',     cardStyle: { backgroundColor: '#FFFFFF' },   },   ** ); 
like image 180
Grigore Budac Avatar answered Oct 02 '22 13:10

Grigore Budac


For React Navigation 5 and above

<Stack.Navigator     initialRouteName='dashboard'     screenOptions={{         headerStyle: { elevation: 0 },         cardStyle: { backgroundColor: '#fff' }     }} >     <Stack.Screen name="Home" component={HomeStack} /> </Stack.Navigator> 

For React Navigation 4 and earlier

const HomeStack = StackNavigator( {     Home: {         screen: HomeScreen,     }, }, {     headerMode: 'screen',     cardStyle: { backgroundColor: '#fff' }, }, ); 
like image 27
Akshay I Avatar answered Oct 02 '22 14:10

Akshay I