Adding a default font when using StackNavigator in React Native

一笑奈何 提交于 2019-12-11 16:19:41

问题


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


来源:https://stackoverflow.com/questions/50081042/adding-a-default-font-when-using-stacknavigator-in-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!