Javascript 检查组件';s样式使用循环
我使用循环创建了一组按钮。这些按钮的背景颜色一个接一个地改变,一次一个按钮。有没有办法在调用函数时获取背景色为黑色的按钮的键Javascript 检查组件';s样式使用循环,javascript,reactjs,loops,react-native,Javascript,Reactjs,Loops,React Native,我使用循环创建了一组按钮。这些按钮的背景颜色一个接一个地改变,一次一个按钮。有没有办法在调用函数时获取背景色为黑色的按钮的键 export default class App extends Component<Props> { constructor(props) { super(props); this.state = { selectedControl: 0, controls: ["TITLE
export default class App extends Component<Props> {
constructor(props) {
super(props);
this.state = {
selectedControl: 0,
controls: ["TITLE1", "TITLE2", "TITLE3"]
};
}
componentDidMount() {
this.timerHandle = setInterval(() => {
this.setState(({selectedControl, controls}) =>
({selectedControl: (selectedControl + 1) % controls.length})
);
}, 1000);
}
render() {
const {selectedControl, controls} = this.state;
return (
<View style={{ flex: 1, flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'space-evenly',
alignItems: 'stretch' }}>
{controls.map((control, index) => (
<Button key={control} title={control} buttonStyle={index === selectedControl ? styles.highlighted : styles.buttonStyle}/>
))}
</View>
);
}
}
const styles = StyleSheet.create({
buttonStyle: {
height: '100%',
},
highlighted: {
height: '100%',
backgroundColor: 'black',
}
});
导出默认类应用程序扩展组件{
建造师(道具){
超级(道具);
此.state={
所选控件:0,
控件:[“标题1”、“标题2”、“标题3”]
};
}
componentDidMount(){
this.timerHandle=setInterval(()=>{
this.setState({selectedControl,controls})=>
({selectedControl:(selectedControl+1)%controls.length})
);
}, 1000);
}
render(){
const{selectedControl,controls}=this.state;
返回(
{controls.map((控件,索引)=>(
))}
);
}
}
const styles=StyleSheet.create({
按钮样式:{
高度:“100%”,
},
突出显示:{
高度:“100%”,
背景颜色:“黑色”,
}
});
按钮键设置为控制
。因此,检查按钮在render
中高亮显示的时间(即index==selectedControl
),按钮的键是control
<View style={{
flex: 1,
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'space-evenly',
alignItems: 'stretch'
}}>
{controls.map((control, index) => {
if (index === selectedControl) {
console.log({"key": control}) /* <-- here */
}
return <Button key={control} title={control} buttonStyle={index === selectedControl ? styles.highlighted : styles.buttonStyle}/>
})}
</View>
{controls.map((控件,索引)=>{
如果(索引===selectedControl){
log({“key”:control})/*