博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
native react 变颜色 点击_React Native主动更改StackNavigator标头颜色
阅读量:1533 次
发布时间:2019-04-21

本文共 3448 字,大约阅读时间需要 11 分钟。

UPDATE:

I'm not making progress with the previous question, so I'm changing it hoping I could find another answer

I'm making an application in React Native, and I'm trying to implement a feature that will change the color of the header and then immediately see the change.

I have a global style sheet that I import and use everywhere in my app

var globalStyles = Stylesheet.create({

menuHex: {

backgroundColor: '#6ed168'

}

...other styles...

})

The menu uses the following code. The Variable 'DrawerStack' on line 2 has all my screens on it, but that's not important. On line 6 I use the variable 'globalStyles.menuHex' that comes from the style sheet in my last code snippet

const DrawerNavigation = StackNavigator({

DrawerStack: {screen: DrawerStack },

}, {

headerMode:'float',

navigationOptions: ({navigation}) => ({

headerStyle: globalStyles.menuHex,

title: 'Application',

headerTintColor: 'black',

headerLeft: {

navigation.navigate('DrawerToggle')

}}>

})

})

I then have this function to change the hex value of the 'menuHex variable'

changetheme(itemValue){

this.setState({theme: itemValue})

if(itemValue === 'spring'){

globalStyles.menuHex = {backgroundColor: '#6ed168'}

}

else if(itemValue === 'summer'){

globalStyles.menuHex = {backgroundColor: '#ffff00'}

}

else if(itemValue === 'autumn'){

globalStyles.menuHex = {backgroundColor: '#ffa500'}

}

else if(itemValue === 'winter'){

globalStyles.menuHex = {backgroundColor: '#ADD8E6'}

}

}

My problem is that when the 'menuHex' variable is changed, the change does not show until after I hit the menu toggle button or until I change pages. I want it so that the color of the header of the menu will be changed when the changetheme() function is complete. I tried running this.forceUpdate(), which did not work

Any more help is appreciated

解决方案

Apologies for making another question similar to this. My previous question was how to get Async Storage to block before continuing, because it wasn't before. Below was my code.

import globalStyles from './src/style'

setStyle = async () => {

try{

const itemValue = await AsyncStorage.getItem('app_theme')

if(itemValue == null){

AsyncStorage.setItem('app_theme', 'spring')

globalStyles.menuHex = {backgroundColor: '#6ed168'}

}

else{

if(itemValue === 'spring'){

globalStyles.menuHex = {backgroundColor: '#6ed168'} }

else if(itemValue === 'summer'){

globalStyles.menuHex = {backgroundColor: '#ffff00'}

}

else if(itemValue === 'autumn'){

globalStyles.menuHex = {backgroundColor: '#ffa500'}

}

else if(itemValue === 'winter'){

globalStyles.menuHex = {backgroundColor: '#ADD8E6'}

}

}

}

catch(error){

console.log(error)

}

};

What I ended up doing was creating a "Loading Page," which ran the function above on startup and then after it was done setting the style.menuHex variable, it changed to the Home

class Loading extends Component{

constructor(props){

super(props);

this.setStyle(props);

}

async setStyle(props){

try{

const itemValue = await AsyncStorage.getItem('app_theme')

console.log(itemValue)

if(itemValue == null){

AsyncStorage.setItem('app_theme', 'spring')

globalStyles.menuHex = {backgroundColor: '#6ed168'}

}

else{

this.changeTheme(itemValue)

}

}

catch(error){

console.log("yup error:\n")

console.log(error)

}

props.navigation.navigate('Home') //***important line. I navigate after done setting variable

};

render(){

return(

Loading

)

}

}

This may not be what some people are looking for, but this method allowed me to fix my original problems.

转载地址:http://onudy.baihongyu.com/

你可能感兴趣的文章
数据库密码过期 怎么修改
查看>>
Oracle密码过期 怎么修改
查看>>
oracle数据库用户密码将要过期处理办法(ORA-28002)
查看>>
POJO式开发
查看>>
java的(PO,VO,TO,BO,DAO,POJO)解释
查看>>
根据当前时间如何找到上月的第一天和最后一天?
查看>>
定义物料管理的容差范围
查看>>
SAP中如何更改供应商账户组
查看>>
如何查询MySql日志
查看>>
ABAP SEARCH 搜索指定字符串
查看>>
条件 CHAIN 语句
查看>>
一般报java.lang.NullPointerException的原因有以下几种
查看>>
对于多个 BAPI一起commit!
查看>>
电商运营到底做什么?你所不知道的电商运营工作
查看>>
电子商务模式都有哪些
查看>>
Mule与Servicemix比较
查看>>
SAP 货物移动 BAPI 的简单使用 BAPI_GOODSMVT_CREATE
查看>>
Mule,目前综合状态最良好的开源ESB方案
查看>>
大洋洲群狼来了! 这是中国篮球学习契机?
查看>>
CTO到底应不应该写代码?听听硅谷大神们怎么说
查看>>