javascript - react 原生 : How to make change in specific item of ScrollView

标签 javascript android ios react-native

我有一个带有嵌套数组的 json 响应。我正在使用 Accordion-Collapse-react-native库以在 UI 中显示此数组。

{
"accountLocationUid": "5f4f8644-f5da-4126-84da-9c35802fd8d4",
"accountUid": "5f5b02a1-beb0-40e8-8553-c62299869dff",
"accountLocationTypeCode": "LOCATION",
"locationName": "First Location",
"children": [{
        "accountLocationUid": "3000b1d7-4da4-45ab-b87c-2d41b867eea0",
        "accountUid": "5f5b02a1-beb0-40e8-8553-c62299869dff",
        "accountLocationTypeCode": "AREA",
        "locationName": "Area 4",
        "parentAccountLocationUid": "5f4f8644-f5da-4126-84da-9c35802fd8d4",
        "allowAddLocation": true,
        "children": [{

            "accountLocationUid": "8606de2a-1af0-4e81-ae11-df36000c4809",
            "accountUid": "5f5b02a1-beb0-40e8-8553-c62299869dff",
            "accountLocationTypeCode": "AREA",
            "locationName": "Area 2",
            "parentAccountLocationUid": "5f4f8644-f5da-4126-84da-9c35802fd8d4",
            "children": []
        }]
    }
]
}
每个项目都有嵌套数组的子数组。我想要做的是当用户点击任何带有嵌套元素的项目时,它应该是展开的,它工作正常但是这样,展开图标也应该变为折叠。当我试图改变它时,它就会改变它的所有元素。
编码我所做的:
const collapseView = (locationArray: any): any => {
return locationArray?.map((locationRight: any, index: number) => (
  <View key={locationRight?.accountLocationUid}>
    <View>
      <Collapse>
        <CollapseHeader>
          <View style={styles.itemRow}>
            {/* {locationRight?.children?.length > 0 ? <View><Text>{expanded ? 'Yes' : "No"}</Text></View> : null} */}
            {locationRight?.children?.length > 0 ? <Icon style={{ marginLeft: -10 }}
              size={24}
              color="black"
              name={expanded ? "chevron-right" : "chevron-down"} /> : null}
            <Text
              // onPress={() => setExpand(!expanded)}
              numberOfLines={1}
              style={styles.locationName}>
              {locationRight?.locationName}
            </Text>
            <PaperButton style={styles.paperButton}
              mode='outlined'
              color='#263238'
              uppercase={false}
              onPress={() => handlePermission(locationRight?.accountLocationUid)}>
              {_.isUndefined(permission) ? 'Allow' : getPermissionValue(locationRight?.accountLocationUid)}
            </PaperButton>
          </View>
          {/* {console.log("child " + locationRight.locationName + " and size is " + locationRight.children.length)} */}
        </CollapseHeader>
        <CollapseBody>
          <View style={{ marginLeft: 15 }}>
            {!_.isNull(JSON.stringify(locationRight?.children) || locationRight.children.length > 0)
              && collapseView(locationRight?.children)}
          </View>
        </CollapseBody>
      </Collapse>
    </View>
  </View>
))
}
使用下面的代码,我正在尝试更改展开和折叠的图标。
{locationRight?.children?.length > 0 ? <Icon style={{ marginLeft: -5 }}
              size={24}
              color="black"
              name={expanded ? "chevron-right" : "chevron-down"} /> : null}
唯一的问题是,如果我扩展为 true,那么图标会更改每个项目,而不是特定的折叠/展开项目。如何获取特定项目的事件?
我正在使用的库是 Accordion-Collapse-react-native .

最佳答案

我必须保存正在折叠的项目的索引或 ID。所以改变onPress方法如下,并创建一个新的状态来存储索引!

//ON TOP
const [currentIndex,setCurrentIndex]=useState(0);
// Down on the text component
onPress={() => setCurrentIndex(index)}
并且在渲染时可以使用索引来检查它是展开还是折叠
name={currentIndex === index ? "chevron-right" : "chevron-down"}

关于javascript - react 原生 : How to make change in specific item of ScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72260944/

相关文章:

android - WebView - 线程

ios: self 在 block 中变为零?

ios - 通知 View 的公共(public)函数

javascript - 两个给定时间戳剩余时间的百分比 Javascript

javascript - MVC Javascript 按钮值不正确

javascript - 我们可以在 Javascript 中实例化 Java 对象吗?

iphone - 格式与字符串 : Cocos2d-x passing in a CCString

javascript - Should.js - 确定多个对象中的单个字段与单个对象之间的相等性

android - 如何在 Android NDK 中播放音频文件(任何格式)?

android - 文字转语音问题