我有以下内容
class LanguageScreen extends Component {
_onPressButton() {
}
render() {
var enButton = <RoundButton
buttonStyle={'black-bordered'}
text={'EN'}
locale={'en'}
selected={true}
style={styles.roundButtonStyle}
onPress={this._onPressButton}
/>
var arButton = <RoundButton
buttonStyle={'golden-gradient'}
text={'ع'}
locale={'ar'}
selected={false}
style={styles.roundButtonStyle}
onPress={this._onPressButton}
/>
return(
<View style={styles.rootViewStyle}>
<View style={styles.buttonContainerRootViewStyle}>
<View style={styles.buttonContainerViewStyle}>
{enButton}
{arButton}
</View>
</View>
<View style={styles.submitButtonContainerViewStyle}>
<Button style={styles.submitButtonStyle}/>
</View>
</View>
);
}
}
当用户按下enButton时,我想更改arButton的样式,反之亦然,为您提供图片,屏幕截图下方的PFA。
基本上,我想一次突出显示一个按钮,可以说用户单击EN,我想突出显示所选元素并从其他元素中删除突出显示。
这是我的RoundButton组件类
class RoundButton extends Component {
constructor(props) {
super(props);
this.state = { isSelected: true === props.selected };
}
onClickListen = () => {
this.setState({
isSelected: !this.state.isSelected
});
this.forceUpdate();
}
render() {
if (this.state.isSelected) {
return this.goldenGradient(this.props);
} else {
return this.blackBordered(this.props)
}
}
goldenGradient(props) {
return(
<TouchableOpacity
style={styles.buttonStyle}
onPress={this.props.onPress}
onPressOut={this.onClickListen}
>
<LinearGradient
colors={['#E9E2B0', '#977743']}
start={{x: 1.0, y: 0.0}}
end={{x: 0.0, y: 1.0}}
style={styles.linearGradient}
>
<Text style={this.props.locale == 'ar' ? styles.goldenGradientTextStyleAr : styles.goldenGradientTextStyle}>
{props.text}
</Text>
</LinearGradient>
</TouchableOpacity>
);
}
blackBordered(props) {
return(
<TouchableOpacity
style={
styles.buttonStyle,
styles.blackBorderedStyle
}
onPress={this.props.onPress}
onPressOut={this.onClickListen}
>
<Text style={this.props.locale == 'ar' ? styles.blackBorderedTextStyleAr : styles.blackBorderedTextStyle}>
{props.text}
</Text>
</TouchableOpacity>
);
}
}
我正在寻找的解决方案是,如果用户单击EN按钮,那么我希望其他按钮也触发按下,这将导致状态更改并切换突出显示状态。似乎没有解决方案。我该怎么办?
答案 0 :(得分:1)
对此的最佳解决方案是让父组件管理按钮高亮显示。因此,您必须将当前选定的按钮置于父组件的状态,然后将布尔属性传递给该按钮,以指示该按钮是否被选中。如我所见,您已经通过了一个“选定的”道具,该道具应该在父组件中处理,而不是在按钮的组件中处理。
如果按照您说的做,您将中断自上而下的数据流,其反应基于
添加构造函数:
constructor(props) {
super(props);
this.state = {
selectedButton: 'en'
};
this._onPressButton = this._onPressButton.bind(this);
}
按下按钮:
_onPressButton(button) {
this.setState({
selectedButton: button
});
}
按钮初始化:
const arButton = <RoundButton
buttonStyle={'golden-gradient'}
text={'ع'}
locale={'ar'}
selected={this.checkButtonSelect('ar')}
style={styles.roundButtonStyle}
onPress={this._onPressButton}/>
const enButton = <RoundButton
buttonStyle={'black-bordered'}
text={'EN'}
locale={'en'}
selected={this.checkButtonSelect('en')}
style={styles.roundButtonStyle}
onPress={this._onPressButton}
检查是否选择了按钮
checkButtonSelect(button) {
return this.state.selectedButton === button;
}
很自我解释
class RoundButton extends Component {
constructor(props) {
super(props);
this.state = { isSelected: true === props.selected };
}
onClickListen = () => {
this.props.onPress(this.props.locale);
/*
* You dont need this, the component is already updated on setState()
* call
*/
//this.forceUpdate();
}
render() {
if (this.state.isSelected) {
return this.goldenGradient(this.props);
} else {
return this.blackBordered(this.props)
}
}
goldenGradient(props) {
return(
<TouchableOpacity
style={styles.buttonStyle}
onPress={this.onClickListen}
>
<LinearGradient
colors={['#E9E2B0', '#977743']}
start={{x: 1.0, y: 0.0}}
end={{x: 0.0, y: 1.0}}
style={styles.linearGradient}
>
<Text style={this.props.locale == 'ar' ? styles.goldenGradientTextStyleAr : styles.goldenGradientTextStyle}>
{props.text}
</Text>
</LinearGradient>
</TouchableOpacity>
);
}
blackBordered(props) {
return(
<TouchableOpacity
style={
styles.buttonStyle,
styles.blackBorderedStyle
}
onPress={this.props.onPress}
onPressOut={this.onClickListen}
>
<Text style={this.props.locale == 'ar' ? styles.blackBorderedTextStyleAr : styles.blackBorderedTextStyle}>
{props.text}
</Text>
</TouchableOpacity>
);
}
}
答案 1 :(得分:1)
您是否考虑过更改呈现按钮的父组件中的状态?为什么不跟踪LanguageScreen
中按下了哪个按钮,然后将此信息传递给按钮。
_onPressButton (selectedLocale) {
this.setState({ selectedLocale })
}
var enButton = <RoundButton
onPress={this._onPressButton}
isSelected={this.state.selectedLocale === 'en'}
...youStaff
/>
var arButton = <RoundButton
onPress={this._onPressButton}
isSelected={this.state.selectedLocale === 'ar'}
...yourStaff
/>
在您的RoundButton
中:
onClickListen = () => {
this.props.onPress(this.props.locale)
}
render() {
if (this.props.isSelected) {
return this.goldenGradient(this.props);
} else {
return this.blackBordered(this.props)
}
}