javascript - 仅处理数组中按下的项目

标签 javascript reactjs react-native

Figure 1 enter image description here

上图显示了我想仅应用于数组中按下的项目的样式类型。

下面的代码是我到目前为止所做的。每当其 sibling 中的任何一个被按下时,数组中的所有项目都会不断获得应用于它们的样式。所以我的问题是,如何将事件(应用样式)集中到唯一按下的同级?此代码用于第二个 gif

import React, { Component } from 'react';
import { View, Text ,StyleSheet, TouchableOpacity, } from 'react-native';
import {connect} from "react-redux"
import {itemSelect} from "../../Store/Actions/index"

 class Selections extends Component {

    state={
        highlighted: false,
        id: null
    }
// The purpose of this function is to set the state to the target index on press
indexStateHandler = (i) =>{

         this.setState({
             id: i
         })

}


    //The purpose of this function is to set styles for the targeted index
    highlightStateHandler = () =>{     

            if(this.state.highlighted == true){ 
                this.setState({
                    highlighted:false
                })

            }
            else{
                this.setState({
                    highlighted:true
                })

            }

           }

    highlightHandler = (i,options) =>{
        this.indexStateHandler(i)
        this.highlightStateHandler()
        console.log("index: "+i)
        console.log(this.state.highlighted)


       // this.props.priceEdit(options)
    }

    componentDidMount = () => {
        console.log(this.state.highlighted)
    };



  render() {
    return (
      <View style={styles.selectionWrapper}>
        <View style={styles.label}><Text style={{color: "black"}}>{this.props.label}</Text></View>
        <View style={styles.listContainer}>
        {this.props.options.map((options, i) => (
            <TouchableOpacity onPress={()=> this.highlightHandler(options.uid, options)} key={i}>
            <View style={this.state.id == options.uid ?styles.highlighted:styles.options} > <Text style={styles.text}>{options.name}</Text> </View>
            </TouchableOpacity>
              )
        )}
        </View>
      </View>
    );
  }
}

const styles= StyleSheet.create({
    selectionWrapper:{
        width: '100%',
        height: 100,
        borderWidth: 1,
        borderColor: 'red',
    },
    label:{
        flex: 1,

    }
    ,
    listContainer:{
        flex: 3,
        flexDirection: "row",
        justifyContent: "space-around",
        alignItems: 'center',
        // backgroundColor: "#7fffd4"
    },
    options:{
        borderRadius: 10,
        padding: 5,
        borderWidth: 1,
        borderColor: "#d0b783",
        // backgroundColor: ""

    },
    text:{
        color: 'black',
        textAlign: 'center'
    },

    highlighted:{
        borderRadius: 10,
        padding: 5,
        borderWidth: 1,
        // borderColor: "#d0b783",
        backgroundColor: "#d0b783"

    }
})

const mapDispatchToProps = dispatch => {
    return{
        select: (bool)=>dispatch(itemSelect(bool))
    }
}

const mapStateToProps = state => {
    return{
        id: state.cart.id
    }

}

export default connect(mapStateToProps,mapDispatchToProps)(Selections)

已更新

我使用元素的索引进行定位。下面是它的代码。 根据 gif,你会发现一切都工作得很好,除了当我第二次按下一个元素时(根据当前示例,这是普通的),由于逻辑原因,它没有重新打开样式。我知道它的行为是这样的,因为当当前索引和前一个索引与您在代码中看到的相同时,状态中的“booln”计算结果为 false。请看一下代码,看看哪里需要改进。

...
state={

    index: null,
    booln:false
}

highlightHandler = (optionIndex) => {

    this.setState(prevState =>({

        index: optionIndex,
        booln:prevState.index == optionIndex? false: true
    }))
}

 render() {
    return (
      <View style={styles.selectionWrapper}>
        <View style={styles.label}><Text style={{color: "black"}}>{this.props.label}</Text></View>
        <View style={styles.listContainer}>
        {this.props.options.map((options, i) => (
            <TouchableOpacity onPress={()=> this.highlightHandler(options.uid, options)} key={i}>
            <View style={this.state.booln&& this.state.index == options.uid? styles.highlighted:styles.options} > <Text style={styles.text}>{options.name}</Text> </View>
            </TouchableOpacity>
              )
        )}
        </View>
      </View>
    );
  }
}
...

最佳答案

而不是改变你的 state.highlightedtruefalse为什么不存储 indexid data的按下,然后在三元运算符中检查 id 是否等于 id存储在 state.highlighted 。此外,您想跟踪多个按下的项目,您可以存储它们的 idsindex在一个数组中并检查它们是否 idindex位于该数组中以应用样式

关于javascript - 仅处理数组中按下的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52813565/

相关文章:

javascript - 当我尝试从 react 查询中破坏数据时,我得到属性 'data' 在类型 'void' 上不存在

javascript - 显示 openweathermap API 错误时出错

javascript - 将鼠标悬停在技能栏上时如何使图像出现

javascript - 有没有一种直接的方法可以让集合上的 jQuery 动画创建一个新的 promise ?

javascript - 日期.值 = 新日期(日期.值);在 IE11 中不起作用

image - 比 IOS 更快地在 android 上响应 native 图像加载

javascript - react native 获取 blob 导致错误

javascript - 为什么这个CSS动画(transform : scale) starting in the wrong location?

javascript - 网格元素在一列而不是一行 Material-UI

node.js - 无法在 react native 教程中启动 npm