使用react native ui小猫为React Native设置主题

时间:2018-10-14 04:43:59

标签: reactjs react-native

我正在尝试使用react-native-ui-kitten来响应本机组件,并且效果很好。 最近,我想在应用程序中添加主题,并为此使用了它们的“ RkTheme”和“ RkType”,但没有一个起作用。

期望-使用RkTheme.setTheme(themeJson)设置主题应该更改已经在屏幕上呈现的元素的样式

实际-使用RkTheme.setTheme(themeJson)设置主题不会更改已经渲染的组件的样式。

以下是json包的一部分

  "dependencies": {
"prop-types": "^15.6.1",
"react": "16.2.0",
"react-dom": "^16.3.1",
"react-native": "0.51.0",
"react-native-linear-gradient": "^2.4.0",
"react-native-popup-menu": "^0.9",
"react-native-ui-kitten": "3.1.2",
"react-native-vector-icons": "^4.6.0",
"react-navigation": "1.0.0-beta.11",
"react-redux": "^5.0.7",
"realm": "^2.15.3",
"redux": "^3.7.2",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.3.0",
"util": "^0.11.0",

},

react组件的示例代码

    import React from 'react'
import { StyleSheet, Text, View, Image, Button , TouchableOpacity} from 'react-native'
import {applyTheme} from '../config/bootstrap'
import {RkStyleSheet, RkTheme} from 'react-native-ui-kitten'
import { LightTheme } from '../config/lightTheme';
import { DarkTheme } from '../config/darkTheme';
export default class Sample extends React.Component {
  constructor(props){
    super(props)
    this.changeTheme = this.changeTheme.bind(this)
  }
  changeTheme(theme){
    if(theme){
      RkTheme.setTheme(DarkTheme,null)
    } else {
      RkTheme.setTheme(LightTheme,null)
    }
  }
  render() {
    return (
      <View style={styles.container}>
        <Text>Sample</Text>
        <TouchableOpacity  onPress = { (e) => this.changeTheme(false) }>
          <Text style={styles2.button1}>Apply Light</Text>
        </TouchableOpacity>
        <TouchableOpacity   onPress = { (e) => this.changeTheme(true) }>
          <Text style={styles2.button1}>Apply Dark</Text>
        </TouchableOpacity>
      </View>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
})
const styles2 = RkStyleSheet.create(theme => ({
  button1 : {
    color : theme.colors.secondaryColor
  }
}))

1 个答案:

答案 0 :(得分:1)

我们需要在我们假定将在更改主题后实时更改主题颜色的组件中添加“ withRkTheme”。还要注意的重要一点是,如果我们在根组件的render函数中使用任何其他自定义组件,则自定义组件应扩展'RkComponent'而不是'React.Component'。独自解决了我的问题。