reactjs - 如何更新 React Native 中的组件?
问题描述
我的问题是我想更改头像的颜色,我使用的是嵌套有堆栈导航器的 DrawNavigation,所以当我单击 DrawNavigation 中的“配置文件”选项时,它会转到配置文件屏幕,在那里我可以看到几个按钮一种不同的颜色,如果我点击一个,头像会根据按钮的颜色改变颜色,但头像的颜色不会改变。
DrawerNavigation 代码的一部分:
import React from "react"
import {View, Text, StyleSheet, TouchableOpacity, Image} from "react-native"
import {DrawerContentScrollView, DrawerItemList} from "@react-navigation/drawer"
import AvatarC from "../components/AvatarC"
export default function UserContent({...props}){
return(
<DrawerContentScrollView {...props} >
<View style={styles.datUser}>
<AvatarC size={60} color={'blue'}/>
<View style={styles.Name}>
<Title style={styles.Title} >Ryan Campbell</Title>
<Caption>ryan@gmail.com</Caption>
</View>
</View>
<DrawerItemList {...props} />
</DrawerContentScrollView>
)
}
配置文件代码的一部分:
import React, {useState} from "react"
import {View, Text, TouchableOpacity, StyleSheet, ScrollView,Dimensions, Image} from "react-native"
import {Avatar, DefaultTheme, } from "react-native-paper"
import AvatarC from "../components/AvatarC"
export default function Perfil(){
const[color, setColor] = useState('blue')
const changeColor = (newColor) =>{
setColor(newColor)
}
return(
<View style={{height:(screenHeight-200), flex:1}}>
<ScrollView showsVerticalScrollIndicator={false} style={styles.viewBody} contentContainerStyle={{flexGrow:1}}>
<View style={{ flex:1}}>
<AvatarC size={60} color={color}/>
<Text style={styles.changeColor}>
Change Color Profile:
</Text>
<View style={styles.viewColors}>
<TouchableOpacity onPress={()=>changeColor('red')} style={{...styles.bnColor, backgroundColor:"red"}}></TouchableOpacity>
<TouchableOpacity onPress={()=>changeColor('green')} style={{...styles.bnColor, backgroundColor:"green"}}></TouchableOpacity>
<TouchableOpacity onPress={()=>changeColor('yellow')} style={{...styles.bnColor, backgroundColor:"yellow"}}></TouchableOpacity>
<TouchableOpacity onPress={()=>changeColor('blue')} style={{...styles.bnColor, backgroundColor:"blue"}}></TouchableOpacity>
<TouchableOpacity onPress={()=>changeColor('pink')} style={{...styles.bnColor, backgroundColor:"pink"}}></TouchableOpacity>
<TouchableOpacity onPress={()=>changeColor('purple')} style={{...styles.bnColor, backgroundColor:"purple"}}></TouchableOpacity>
<TouchableOpacity onPress={()=>changeColor('orange')} style={{...styles.bnColor, backgroundColor:"orange"}}></TouchableOpacity>
<TouchableOpacity onPress={()=>changeColor('brown')} style={{...styles.bnColor, backgroundColor:"brown"}}></TouchableOpacity>
<TouchableOpacity onPress={()=>changeColor('black')} style={{...styles.bnColor, backgroundColor:"black"}}></TouchableOpacity>
<TouchableOpacity onPress={()=>changeColor('#001269')} style={{...styles.bnColor, backgroundColor:"#001269"}}></TouchableOpacity>
</View
</View>
</ScrollView>
</View>
);
}
组件代码:
import React, {useState, useEffect} from "react"
import {Avatar, DefaultTheme, } from "react-native-paper"
export default function AvatarC(props){
const[color, setColor] = useState('blue')
useEffect( () => {
changeC()
}, [props.color])
const colorP = props.color
var theme = {
...DefaultTheme,
roundness: 2,
colors: {
...DefaultTheme.colors,
primary: color,
},
};
const changeC =()=>{
setColor(props.color)
}
return(
<Avatar.Text
size={props.size}
label={"RC"}
theme={theme}
/>
)
}
解决方案
我认为你必须只关注组件中的道具
import {Avatar, DefaultTheme} from "react-native-paper"
export default function AvatarC(props){
var theme = {
...DefaultTheme,
roundness: 2,
colors: {
primary: props.color,
},
};
return(
<Avatar.Text
size={props.size}
label={"RC"}
theme={theme}
/>
)}
推荐阅读
- iis - 如何在 .Net Core 2 中为 IIS 发布定义自定义 web.config?
- javascript - 在 react-table 中设置 selectTable 标头的样式
- rust - Rust 中的重复 trait 方法实现
- javascript - 在 javascript map es6 中传递附加参数
- java - @PropertySource 中的 File.separator
- java - Spring Security:带有 PersistentTokenBasedRememberMeServices 的 CookieTheftException
- angular - get/set 已被弃用,取而代之的是 config 命令
- mysql - mysql - 按 INTERVAL 1 HOUR 分组(检测可疑的批量登录)
- c# - 如何初始化复数数组,以便显示相位、幅度等属性
- java - 添加 Firebase SDK 后无法将 Web 应用程序部署到 GlassFish 5/Payara 5:“com/google/firebase/FirebaseOptions$Builder”