Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

嵌套列表的 function 取值问题 #2551

Closed
taichenglu opened this issue Mar 22, 2019 · 5 comments
Closed

嵌套列表的 function 取值问题 #2551

taichenglu opened this issue Mar 22, 2019 · 5 comments
Assignees

Comments

@taichenglu
Copy link

这样打印出来的始终是 id 2,是 bug 吗?还是我姿势有问题?
taro 版本:v1.2.21

image

clickId = (subId) => {
  console.log(subId)
}

render () {
  const data = [{
    subData: [{
      id: 1
    }]
  }, {
    subData: [{
      id: 2
    }]
  }]

  return (
    <View>
      {data.map((parent, i) =>
        <View key={String(i)}>
          {parent.subData.map((sub, j) =>
            <View>
              <View key={String(j)} onClick={() => this.clickId(sub.id)}>
                点击了id{sub.id}
              </View>
            </View>
          )}
        </View>
      )}
    </View>
  )
}
@taro-bot
Copy link

taro-bot bot commented Mar 22, 2019

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

@taro-bot
Copy link

taro-bot bot commented Mar 22, 2019

CC @yuche

@taro-bot taro-bot bot assigned yuche Mar 22, 2019
@taichenglu
Copy link
Author

我本来是想用 navigateTo

gotoDetail = (id) => {
  Taro.navigateTo({
    url: `/pages/detail/detail?id=${id}`
  })
}

换成 <Navigator /> 组件来实现倒是没有问题,不过这看上去确实是个 bug。

@yuche
Copy link
Contributor

yuche commented Mar 22, 2019

多重循环用匿名函数有问题,你可以先用 bind 代替

@taichenglu
Copy link
Author

@yuche 嗯嗯,改成 bind 是好的
onClick={this.clickId.bind(this, sub.id)}

只是 eslint 要关掉 react/jsx-no-bind 了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants