Adding a default font when using StackNavigator in

2019-08-31 05:01发布

问题:

I am trying to set a default font by following the code by robertmylne on this page. However, it tells me to place the code in my constructor in my App.js. The problem is that I am using a StackNavigator and as far as I know I cannot use a constructor. My code looks something like this:

import Module1 from './components/Module1'
import Module2 from './components/Module2'
import Module3 from './components/Module3'

const App = StackNavigator(
    {
        Module1: { screen: Module1 },
        Module2: { screen: Module2 },
        Module3: { screen: Module3 }
    },
    { headerMode: 'none'}
)

export default App

Is there some way to get the relevant code working when using a StackNavigator?

回答1:

StackNavigator is a HOC which returns a component. You can use it as any other component.

Example

import Module1 from './components/Module1'
import Module2 from './components/Module2'
import Module3 from './components/Module3'

const Navigator = StackNavigator(
    {
        Module1: { screen: Module1 },
        Module2: { screen: Module2 },
        Module3: { screen: Module3 }
    },
    { headerMode: 'none'}
)

class App extends Component {
  constructor(props) {
    super(props)
    // any other code you need to add
  }

  render() {
    return <Navigator />
  }
}

export default App