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

setState设置二维数组与原微信小程序内设置结果不一致 #3098

Closed
yangguo8466 opened this issue May 20, 2019 · 15 comments
Closed
Assignees
Labels
question Further information is requested

Comments

@yangguo8466
Copy link

let pages =[]
let value = ['inner']
预期设置后 pages[['inner']]
实际在setState后变为pages[0]:[inner]
而在原生微信小程序下setData 是与预期一致 的,设置方法如下
setData({
[pages[0]]:value
})

@taro-bot
Copy link

taro-bot bot commented May 20, 2019

欢迎提交 Issue~

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

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

Good luck and happy coding~

@Chen-jj Chen-jj self-assigned this May 21, 2019
@Chen-jj
Copy link
Contributor

Chen-jj commented May 21, 2019

@GYDSB 提供一下完整可复现代码

@yangguo8466
Copy link
Author

this.setData({
[pages[0]]:value
})
在原生小程序下结果为pages:[[value]]
this.setState({
[pages[0]]:value
})
设置后在调试器显示为pages[0]:value,pages:[]而非pages:[[value]]

@yangguo8466
Copy link
Author

@Chen-jj

@Chen-jj
Copy link
Contributor

Chen-jj commented May 21, 2019

@GYDSB 你这样写,setState 表现正常吧,再者原生小程序你确定是 pages:[[value]] ??

@yangguo8466
Copy link
Author

taro上按上方式控制台输出
pages: []
pages[0]: ["test"]且调试器AppData显示pages:[]
原生setData 输出
pages: [["test"]]且调试器AppData显示pages:[["test"]] @Chen-jj

@yangguo8466
Copy link
Author

-。-应该是直接吧pages[0]当成key了,导致表现有点不一致

@Chen-jj
Copy link
Contributor

Chen-jj commented May 22, 2019

@GYDSB 当成 key ,是正确的。

@yangguo8466
Copy link
Author

@Chen-jj -。-所以不一致了~

@Chen-jj
Copy link
Contributor

Chen-jj commented May 23, 2019

@GYDSB JS 里面这样写不正确吗?

@yangguo8466
Copy link
Author

正确,但是跟期望的表现不一致 @Chen-jj 期望的是更新pages某项值,不是新增一个’pages[0]‘属性的值

@Chen-jj
Copy link
Contributor

Chen-jj commented May 24, 2019

@GYDSB 我的意思是你写法有误。

@yangguo8466
Copy link
Author

yangguo8466 commented May 26, 2019

this.setState({
[`pages[0]`]:value
})这样没问题吧 @Chen-jj

@Chen-jj
Copy link
Contributor

Chen-jj commented May 27, 2019

@GYDSB 你这种写法,是指用 pages[0] 作为 key,而不是修改 pages 的第一项。。。。你先去 react playground 了解一下 ES6 的属性名表达式和 setState 用法吧。

@Chen-jj Chen-jj added answered question Further information is requested labels May 27, 2019
@taro-bot
Copy link

taro-bot bot commented May 27, 2019

Hello~

您的问题楼上已经有了确切的回答,如果没有更多的问题这个 issue 将在 15 天后被自动关闭。

如果您在这 15 天中更新更多信息自动关闭的流程会自动取消,如有其他问题也可以发起新的 Issue。

Good luck and happy coding~

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

No branches or pull requests

2 participants