javascript - 当我映射对象数组时如何突出显示多个项目?

标签 javascript arrays reactjs react-native

我试图弄清楚如何突出显示所选的多个对象,或者更好的是调用我在代码中插入的复选标记。我的零食在这里:

https://snack.expo.io/@fauslyfox110/testingreferrals

具体映射在这里:

const ContactRow = React.memo(
  ({ onPress, name, email, phone, selected }: ContactRowProps) => {
    return (

      <TouchableHighlight onPress={onPress}>
        <View
          style={{ padding: 16, flexDirection: 'row', alignItems: 'center' }}>
             <Text style={{ marginRight: 16 }}>{selected ? '✅' : '⭕️'}</Text>
            <View style={{ flex: 1 }}>
              <Text>{name}</Text>
              {name.length > 0 && (
                <View>
                <Text style={{ marginTop: 4, color: '#666' }}>
                  {email}
                </Text>
                <Text style={{ marginTop: 4, color: '#666' }}>
                  {phone}
                </Text>
                </View>
              )}
            </View>
          </View>
      </TouchableHighlight>

          );
      }
    );

我从手机中提取联系人,然后映射它们以将它们呈现为列表。我可以选择多个项目并传递要在按钮中呈现的数字,但我希望它们切换到复选标记或更改颜色以帮助用户。

最佳答案

我注意到您没有将“selected”属性传递给 ContactRow。

<ContactRow
    onPress={ ()=> this.addSelectedContact({id: contact.name,name: contact.name, email:contact.email, phone:contact.phone}) }
    name={ contact.name } 
    emailOrNumber={ contact.email || contact.phone }
/>

更改为

<ContactRow
    onPress={ ()=> this.addSelectedContact({id: contact.name,name: contact.name, email:contact.email, phone:contact.phone}) }
    name={ contact.name } 
    emailOrNumber={ contact.email || contact.phone }
    selected={Boolean(this.state.selectedContacts.find(item => item.name === contact.name && item.phone === contact.phone)}
/>

关于javascript - 当我映射对象数组时如何突出显示多个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59463926/

相关文章:

javascript - 返回对象列表时在 ajax 中检索数据时出错

c++ - 如何在不使用循环的情况下快速打印由指向指针的指针组成的二维字符数组?

java - 如何从 int 数组向下舍入到最接近的整数?

reactjs - 类型错误 : JSX element type '{} | null | undefined' is not a constructor function for JSX elements

javascript - Webpack 包未导出且无法导入

javascript - 如何使用 Angularjs Watch 获取 newVal

javascript - 如何使用 JavaScript 检查下拉值

javascript - 使用 React.js 的 onClick 按钮在 url 上增加 ID

arrays - 从单元格的单元格数组中检索第一个值

node.js - CORS 阻止来自 Dockerized React 项目的 API 调用