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改变数组中的某一项或者某一属性没有生效 #2278

Closed
jyjey opened this issue Feb 27, 2019 · 8 comments
Closed
Assignees
Labels
question Further information is requested

Comments

@jyjey
Copy link

jyjey commented Feb 27, 2019

问题描述
由于微信小程序的限制一次setState不能超过1024kb,按微信官方文档逐个set数组中的元素,无效。
原生微信小程序代码可以实现。

[或者可以直接贴源代码,能贴文字就不要截图]

setOrderData = (orderData, index) => {
    if(orderData.length > index){
      let key = "orderData["+index+"]";
      this.setState({
        [key]: orderData[index]
      }, () => {
        this.setOrderData(orderData, index + 1)
      })
    }
  };

期望行为
能正常改变数组中的某一项或者某一属性

报错信息

无报错信息,state中没有数据变化

系统信息

Taro CLI 1.2.14 environment info:
System:
OS: Windows 10
Binaries:
Node: 10.15.1 - D:\workTool\nodejs\node.EXE
Yarn: 1.13.0 - C:\Users\ACE\AppData\Roaming\npm\yarn.CMD
npm: 6.4.1 - D:\workTool\nodejs\npm.CMD

@taro-bot
Copy link

taro-bot bot commented Feb 27, 2019

欢迎提交 Issue~

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

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

Good luck and happy coding~

@Chen-jj Chen-jj self-assigned this Feb 27, 2019
@Chen-jj
Copy link
Contributor

Chen-jj commented Feb 27, 2019

@jyjey 提供一下可复现代码

@jyjey
Copy link
Author

jyjey commented Feb 27, 2019

import Taro, { Component, Config } from '@tarojs/taro'
import {View, Text, Image} from '@tarojs/components'
import {AtNavBar, AtTabs, AtTabsPane, AtList, AtListItem, AtButton, AtIcon} from 'taro-ui'

export default class Order extends Component {

  /**
   * 指定config的类型声明为: Taro.Config
   *
   * 由于 typescript 对于 object 类型推导只能推出 Key 的基本类型
   * 对于像 navigationBarTextStyle: 'black' 这样的推导出的类型是 string
   * 提示和声明 navigationBarTextStyle: 'black' | 'white' 类型冲突, 需要显示声明类型
   */
  config: Config = {
    navigationBarTitleText: '订单'
  };
  openid = '';
  constructor () {
    super(...arguments)
  }


  state = {
    orderData:[]
  };



  setOrderData = (orderData, index) => {
    if(orderData.length > index){
      let key = "orderData["+index+"]";
      this.setState({
        [key]: orderData[index]
      }, () => {
        this.setOrderData(orderData, index + 1)
      })
    }
  };


  

  componentWillMount ()
  {
    let orderData = ["Strawberry", "Banana", "Mango"];
	this.setOrderData(orderData, 0);
  }

  componentDidMount () { }

  componentWillUnmount () { }

  componentDidShow () { }

  componentDidHide () { }



  render () {
    const {orderData} = this.state;
	console.log(orderData); //这里并没有更新到orderData
    return (
      <View>

      </View>
    )
  }
}

@jyjey
Copy link
Author

jyjey commented Feb 27, 2019

@Chen-jj

@Chen-jj
Copy link
Contributor

Chen-jj commented Mar 5, 2019

@jyjey Taro 在小程序 setData 前,会做一次 diff ,你正常 setState 即可

@Chen-jj Chen-jj added question Further information is requested answered labels Mar 5, 2019
@Chen-jj
Copy link
Contributor

Chen-jj commented Mar 5, 2019

#882

@taro-bot taro-bot bot added the to be closed label Mar 5, 2019
@taro-bot
Copy link

taro-bot bot commented Mar 5, 2019

Hello~

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

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

Good luck and happy coding~

@jyjey
Copy link
Author

jyjey commented Mar 6, 2019

@jyjey Taro 在小程序 setData 前,会做一次 diff ,你正常 setState 即可

感谢

@jyjey jyjey closed this as completed Mar 6, 2019
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