javascript - 在 react-native 中查找组件的特定子项

标签 javascript reactjs react-native badge

我正在创建像这样的徽章或筹码 enter image description here

使用代码:

<View style = {{flexDirection: 'row', marginLeft: 10, marginTop: 5}}>
{this.state.eduModes.map((v, i) => {
  return (
      <Badge
        key={i}
        onPress = {(i) => {
          console.log(i);
        }}
        value={v}
        containerStyle= {{marginRight: 5}}
        textStyle={{ color: 'orange' }}
      />
  )
})}
</View>

用户从创建徽章的选择器中选择值,现在我想要的是当用户点击徽章时应该删除徽章。那么我如何访问用户点击的特定徽章,以便它在重新呈现时消失?

最佳答案

您可以创建一个新的内联函数,将应删除的徽章索引发送到删除函数。

示例

class App extends React.Component {
  handlePress = index => {
    this.setState(previousState => {
      const eduModes = [...previousState.eduModes];
      eduModes.splice(index, 1);
      return { eduModes };
    });
  };

  render() {
    return (
      <View style={{ flexDirection: "row", marginLeft: 10, marginTop: 5 }}>
        {this.state.eduModes.map((v, i) => {
          return (
            <Badge
              key={i}
              onPress={() => this.handlePress(i)}
              value={v}
              containerStyle={{ marginRight: 5 }}
              textStyle={{ color: "orange" }}
            />
          );
        })}
      </View>
    );
  }
}

关于javascript - 在 react-native 中查找组件的特定子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51586876/

相关文章:

javascript - 无法使用状态内的数组初始化 React 复选框

reactjs - 如何使用 jest 模拟类和命名空间枚举?

javascript - 单击按钮时如何将页面向下移动滚动条到特定坐标

android - react native :how to understand device is rooted?

c# - 单击 IFrame 内容按钮时附加代码?

javascript - 在 Redux : How can I change the state of a boolean within an array? 上

android - React-native run-android 在 :app:processDebugResources 上失败

react-native-router-flux tabs 如何更改所选选项卡的图标?

javascript - 您如何获得大小相同的社交媒体按钮?

javascript - 有没有办法使用 Rspec/Capybara/Selenium 将 javascript console.errors 打印到终端?