native react 变颜色 点击_React Native主动更改StackNavigator标头颜色
发布日期:2021-06-24 11:31:03 浏览次数:5 分类:技术文章

本文共 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.

转载地址:https://blog.csdn.net/weixin_32512381/article/details/112818556 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:prism项目搭建 wpf_WPF MVVM使用prism4.1搭建
下一篇:哪个app最费电_关于石墨烯地暖,用户最关心的6大问题

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月04日 13时35分07秒