以编程方式触发按下
Trigger Press Programmatically
我有以下组件
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,我想突出显示所选元素并从其他元素中删除突出显示。
这是我的圆形按钮组件class
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 按钮,那么我希望其他按钮也触发按下,这将导致状态改变并切换突出显示状态。似乎没有解决方案有效。我该怎么做?
最好的解决办法是让父组件管理按钮高亮。因此,您必须将当前选中的按钮放入父组件的状态,并将布尔属性传递给按钮以指示它是否被选中。如我所见,您已经传递了一个 'selected' 道具,该道具应该在父组件中处理,而不是在按钮的组件中处理。
如果按照你说的去做,你会打断react基于
的自上而下的数据流
更新
父组件
添加构造函数:
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>
);
}
}
您是否考虑过更改呈现按钮的父组件中的状态?为什么不跟踪在 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)
}
}
我有以下组件
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,我想突出显示所选元素并从其他元素中删除突出显示。
这是我的圆形按钮组件class
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 按钮,那么我希望其他按钮也触发按下,这将导致状态改变并切换突出显示状态。似乎没有解决方案有效。我该怎么做?
最好的解决办法是让父组件管理按钮高亮。因此,您必须将当前选中的按钮放入父组件的状态,并将布尔属性传递给按钮以指示它是否被选中。如我所见,您已经传递了一个 'selected' 道具,该道具应该在父组件中处理,而不是在按钮的组件中处理。
如果按照你说的去做,你会打断react基于
的自上而下的数据流更新
父组件
添加构造函数:
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>
);
}
}
您是否考虑过更改呈现按钮的父组件中的状态?为什么不跟踪在 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)
}
}