Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查组件';s样式使用循环_Javascript_Reactjs_Loops_React Native - Fatal编程技术网

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})/*