question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use Themeprovider in react-navigation

See original GitHub issue

In my case, i want to manage header backgroundColor with ThemeProvider , how to use them in navigationOptions, example code below :

/**
 * @flow
 */

import React from 'react'
import { Text, View, StatusBar } from 'react-native'
import styled, { withTheme } from 'styled-components/native'
import Ionicons from 'react-native-vector-icons/Ionicons'

let HeaderTitle = styled.TouchableOpacity`
  width:100%;
  height: 30;
  align-self: center;
`
let SearchBar = styled.View`
  height: 30;
  border-radius:30;
  background: #fff;
  flex-direction:row;
  align-items:center;
  justify-content:center;
`
let SearchBarText = styled.Text`
 
`
let HeaderLeft = styled.TouchableOpacity`
  padding-horizontal: 20;
`
let HeaderRight = styled.TouchableOpacity`
  padding-horizontal: 20;
`
class HomeScreen extends React.Component {
  static navigationOptions =({navigation, screenProps}) => ({
    headerStyle: {
      backgroundColor: '#E5466E'
    },
    headerTitle: (
      <HeaderTitle>
        <SearchBar>
          <Ionicons
            name={'ios-search'}
            size={16}
            style={{
              paddingRight: 10,
              color: '#999'
            }}
          />
          <SearchBarText>search</SearchBarText>
        </SearchBar>
      </HeaderTitle>
    ),
    headerRight: (
      <HeaderRight>
        <Ionicons
          name={'ios-mail-outline'}
          size={24}
          style={{
            color: '#fff'
          }}
        />
      </HeaderRight>
    ),
    headerLeft: (
      <HeaderLeft>
        <Ionicons
          name={'md-aperture'}
          size={21}
          style={{
            color: '#fff'
          }}
        />
      </HeaderLeft>
    )
  })
  render () {
    console.log('render', this.props)
    return (
      <View>
        <StatusBar
          barStyle='light-content'
        />
        <Text>首页</Text>
      </View>
    )
  }
}
export default withTheme(HomeScreen)

in navigationOptions i want use theme like this

headerStyle: {
      backgroundColor: theme.primaryColor
    },

instead of

headerStyle: {
      backgroundColor: '#E5466E'
    },

I need some help 🆘

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
k15acommented, May 19, 2017

Ahh sorry I know see that you already applied withTheme. I have never used react-navigation but it seems that there is an extra screenProps which you can use to apply some additional props. As far as I know you have to apply them when you render HomeScreen.

class HomeScreen extends React.Component {
  static navigationOptions =({ navigation, screenProps }) => ({
    headerStyle: {
      backgroundColor: screenProps.theme.yourColor
    }
  )}
}

const Navigation = StackNavigator({
  HomeScreen,
})

export default withTheme(({ theme }) => {
  return <Navigation screenProps={{ theme }} />
})

There might be a better solution for it but this is the only one I am aware of.

4reactions
elprupcommented, Jul 17, 2019

updated to v3:

class HomeScreen extends React.Component {
  static navigationOptions =({ navigation, screenProps }) => ({
    headerStyle: {
      backgroundColor: screenProps.theme.yourColor
    }
  )}
}

const Navigation = createStackNavigator({
  HomeScreen,
})

const AppContainer = createAppContainer(Navigation);
const AppContainerWithTheme = withTheme(({ theme }) => {
  return <AppContainer screenProps={{ theme }} />;
});

// other file using navigation
<AppContainerWithTheme 
      ref={mynav => {
       this.navigator = mynav;
      }}
/>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Themes - React Navigation
Themes allow you to change the colors of various components provided by React Navigation. You can use themes to: Customize the colors match...
Read more >
Theming with React Navigation · React Native Paper
In this guide we will look into how to apply theming for an application using React Native Paper and React Navigation at the...
Read more >
react-native-navigation theming with styled-components
import { Provider } from 'react-redux'; import { ThemeProvider } from 'styled-components'; import theme from './theme'; const Provider = ({ ...
Read more >
Simple React Native Theme Provider | by Ike Njoku - Medium
js (shown above containing all themes we want to make available), color palettes are created that will set the theme of the application...
Read more >
Theming React Native Applications with Styled Components
Detailed tutorial on adding theming to your React Native applications ... To run the application, navigate to the project directory in a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found