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

state数据发生变化,视图没有同步改变 #3813

Closed
ghost opened this issue Jul 14, 2019 · 8 comments
Closed

state数据发生变化,视图没有同步改变 #3813

ghost opened this issue Jul 14, 2019 · 8 comments
Assignees

Comments

@ghost
Copy link

ghost commented Jul 14, 2019

问题描述
state数据和视图没有同步改变

复现步骤
[复现问题的步骤]

  1. 初始化taro项目
  2. 代码如下
export default class Index extends Component {
    config = {
        navigationBarTitleText: '首页'
    }
    state = {
        list: []
    }
    componentDidMount() {
        setTimeout(() => {
            this.setState({list: [
                { id: 'a', text: 0 },
                { id: 'b', text: 1 },
                { id: 'c', text: 2 },
            ]})
        }, 300)
    }
    renderList() {
        console.log(this.state.list)
        if(this.state.list.length > 0) {
            return this.state.list.map(listItem => {
                return (
                    <View key={listItem.id}>
                        <Text>{listItem.text}</Text>
                    </View>
                )
            })
        }
        return (
            <View>
                <Text>empty</Text>
            </View>
        )
    }
    render() {
        return (
            <View className='index'>
                {this.renderList()}
            </View>
        )
    }
}
{
  "name": "taro-test",
  "version": "1.0.0",
  "private": true,
  "description": "",
  "templateInfo": {
    "name": "default",
    "typescript": false,
    "css": "sass"
  },
  "scripts": {
    "build:weapp": "taro build --type weapp",
    "build:swan": "taro build --type swan",
    "build:alipay": "taro build --type alipay",
    "build:tt": "taro build --type tt",
    "build:h5": "taro build --type h5",
    "build:rn": "taro build --type rn",
    "dev:weapp": "npm run build:weapp -- --watch",
    "dev:swan": "npm run build:swan -- --watch",
    "dev:alipay": "npm run build:alipay -- --watch",
    "dev:tt": "npm run build:tt -- --watch",
    "dev:h5": "npm run build:h5 -- --watch",
    "dev:rn": "npm run build:rn -- --watch"
  },
  "author": "",
  "license": "MIT",
  "dependencies": {
    "@tarojs/components": "1.3.0",
    "@tarojs/router": "1.3.0",
    "@tarojs/taro": "1.3.0",
    "@tarojs/taro-alipay": "1.3.0",
    "@tarojs/taro-h5": "1.3.0",
    "@tarojs/taro-swan": "1.3.0",
    "@tarojs/taro-tt": "1.3.0",
    "@tarojs/taro-weapp": "1.3.0",
    "nervjs": "^1.4.0",
    "nerv-devtools": "^1.4.0"
  },
  "devDependencies": {
    "@types/react": "^16.4.6",
    "@types/webpack-env": "^1.13.6",
    "@tarojs/plugin-babel": "1.3.0",
    "@tarojs/plugin-csso": "1.3.0",
    "@tarojs/plugin-sass": "1.3.0",
    "@tarojs/plugin-uglifyjs": "1.3.0",
    "@tarojs/webpack-runner": "1.3.0",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-plugin-transform-jsx-stylesheet": "^0.6.5",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-eslint": "^8.2.3",
    "eslint": "^5.16.0",
    "eslint-config-taro": "1.3.0",
    "eslint-plugin-react": "^7.8.2",
    "eslint-plugin-import": "^2.12.0",
    "stylelint": "9.3.0",
    "stylelint-config-taro-rn": "1.3.0",
    "stylelint-taro-rn": "1.3.0",
    "eslint-plugin-taro": "1.3.0"
  }
}

期望行为
通过setState更新state后,视图同步发生变化

报错信息


系统信息

Taro CLI 1.3.0 environment info:
    System:
      OS: macOS High Sierra 10.13.6
      Shell: 3.2.57 - /bin/bash
    Binaries:
      Node: 10.14.2 - /usr/local/bin/node
      Yarn: 1.16.0 - /usr/local/bin/yarn
      npm: 6.9.0 - /usr/local/bin/npm
    npmGlobalPackages:
      typescript: 3.5.2

补充信息
修改代码为先判断数组为空返回空状态组件会产生图二的错误

@taro-bot
Copy link

taro-bot bot commented Jul 14, 2019

欢迎提交 Issue~

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

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

Good luck and happy coding~

@taro-bot
Copy link

taro-bot bot commented Jul 15, 2019

CC @yuche

@yuche
Copy link
Contributor

yuche commented Jul 15, 2019

没看懂你的描述,是你这部分代码编译后直接就报图上的错,本来不报错,后来改了报错,「修改代码为先判断数组为空」的代码是怎么样的

@ghost
Copy link
Author

ghost commented Jul 15, 2019

没看懂你的描述,是你这部分代码编译后直接就报图上的错,本来不报错,后来改了报错,「修改代码为先判断数组为空」的代码是怎么样的

1. 贴的代码先判断数组长度大于0渲染列表,否则返回空组件;
2. 图二里的代码是先判断数组不大于零返回空组件,否则渲染列表;
1里的代码通过setstate改变数组长度,根据console显示代码是正常运行的,但是视图没更新;
2里调整判断数组是否回空的先后顺序会产生图二的错误;

@yuche
Copy link
Contributor

yuche commented Jul 15, 2019

「调整判断数组是否回空的先后顺序」是怎么样的代码

@ghost
Copy link
Author

ghost commented Jul 15, 2019

「调整判断数组是否回空的先后顺序」是怎么样的代码

图二的右上角的代码

@yuche
Copy link
Contributor

yuche commented Jul 15, 2019

你先把 list 解构出来再用可以解决问题 1

@ghost
Copy link
Author

ghost commented Jul 15, 2019

你先把 list 解构出来再用可以解决问题 1

刚测了一下问题一可以解决,表示感谢。

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