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

数组多次filter时,会改变原数组,求助! #4983

Closed
luoanyang opened this issue Dec 2, 2019 · 7 comments
Closed

数组多次filter时,会改变原数组,求助! #4983

luoanyang opened this issue Dec 2, 2019 · 7 comments
Assignees

Comments

@luoanyang
Copy link

问题描述

本人刚使用taro,开发时遇到问题,自己设置了一个原始数据的字段 originList,显示的是根据 originList.filter(i=>i.type=='状态')得到的 list,但是切换状态的时候会改变originList

复现步骤

  1. 到首页
  2. 点击 “已完成”, 再点击“未完成” ,再点击“已完成”,再点击未完成。
  3. 会看到原本未完成只有2条数据,但是页面显示3条数据.
import Taro, { Component, Config } from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import './index.scss'

export default class Index extends Component {

  state = {
    originList: [
      {
        "_id": "114f807c5de3f4cd0114756633997049",
        "category": "个人",
        "content": "3",
        "date": "2019-12-02",
        "openId": "oOVil5Aj0WeYWLsejVx67Y0twwow",
        "type": "未完成",
        "time": "00:00",
        "tip": false
      },
      {
        "_id": "114f807c5de3f4d8011475eb578fb117",
        "category": "个人",
        "content": "2",
        "date": "2019-12-02",
        "openId": "oOVil5Aj0WeYWLsejVx67Y0twwow",
        "type": "已完成",
        "time": "00:00",
        "tip": false
      },
      {
        "_id": "b3b086ef5de3f4e40113fcf342df4dce",
        "category": "个人",
        "content": "1",
        "date": "2019-12-02",
        "openId": "oOVil5Aj0WeYWLsejVx67Y0twwow",
        "type": "未完成",
        "time": "00:00",
        "tip": false
      }
    ],
    type: '未完成'
  }
  config: Config = {
    navigationBarTitleText: '首页'
  }

  componentDidShow() { 
    this.setState({
      list: this.state.originList.filter(i => i.type === this.state.type)
    });
  }

  changeType(type) {
    this.setState({
      list: this.state.originList.filter(i => i.type === type),
      type
    });
  }

  render() {
    return (
      <View className='index'>
        <View style='overflow:hidden;marign-bottom:30px'>
          <View style={`float:left;margin-right:50px;${this.state.type=='未完成'?'color:red':''}`} onClick={() => this.changeType('未完成')}>未完成</View>
          <View style={`float:left;margin-right:50px;${this.state.type=='已完成'?'color:red':''}`} onClick={() => this.changeType('已完成')}>已完成</View>
        </View>

        {
          this.state.list.map(i => {
            return <View>{i.content}</View>
          })
        }
      </View>
    )
  }
}

期望行为

实际应该 “未完成” 应该一直有 2 条数据,“已完成” 只有1条数据

报错信息

无,就是 originList的数据好像被修改了

系统信息

👽 Taro v1.3.26

Taro CLI 1.3.26 environment info:
System:
OS: macOS 10.15
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 10.15.3 - /usr/local/bin/node
Yarn: 1.16.0 - ~/.npm-global/bin/yarn
npm: 6.9.0 - ~/.npm-global/bin/npm
npmPackages:
@tarojs/components: 1.3.26 => 1.3.26
@tarojs/plugin-babel: 1.3.26 => 1.3.26
@tarojs/plugin-csso: 1.3.26 => 1.3.26
@tarojs/plugin-sass: 1.3.26 => 1.3.26
@tarojs/plugin-uglifyjs: 1.3.26 => 1.3.26
@tarojs/router: 1.3.26 => 1.3.26
@tarojs/taro: 1.3.26 => 1.3.26
@tarojs/taro-alipay: 1.3.26 => 1.3.26
@tarojs/taro-h5: 1.3.26 => 1.3.26
@tarojs/taro-qq: 1.3.26 => 1.3.26
@tarojs/taro-quickapp: 1.3.26 => 1.3.26
@tarojs/taro-swan: 1.3.26 => 1.3.26
@tarojs/taro-tt: 1.3.26 => 1.3.26
@tarojs/taro-weapp: 1.3.26 => 1.3.26
@tarojs/webpack-runner: 1.3.26 => 1.3.26
eslint-config-taro: 1.3.26 => 1.3.26
eslint-plugin-taro: 1.3.26 => 1.3.26
nerv-devtools: ^1.5.0 => 1.5.6
nervjs: ^1.5.0 => 1.5.6
stylelint-config-taro-rn: 1.3.26 => 1.3.26
stylelint-taro-rn: 1.3.26 => 1.3.26
npmGlobalPackages:
typescript: 3.5.3

@taro-bot
Copy link

taro-bot bot commented Dec 2, 2019

CC @Chen-jj

@taro-bot
Copy link

taro-bot bot commented Dec 2, 2019

欢迎提交 Issue~

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

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

Good luck and happy coding~

@shenghanqin
Copy link
Collaborator

这个是在正式项目已经写了呗?
你先试试看,[].concat(orginList).filter(),然后再等待这个问题的解答。

@luoanyang
Copy link
Author

这个是在正式项目已经写了呗?
你先试试看,[].concat(orginList).filter(),然后再等待这个问题的解答。

还没正式项目用,准备用的,写demo出现这个问题,才问下的。你说的那个,我试过了还是不行

@shenghanqin shenghanqin changed the title 数组多次filter时,会改变原数组,求助!急急急 数组多次filter时,会改变原数组,求助! Dec 2, 2019
@shenghanqin
Copy link
Collaborator

哎呀呀,那就不是那么着急了。嘻嘻

@luoanyang
Copy link
Author

哎呀呀,那就不是那么着急了。嘻嘻
好吧,这个是框架自己有额外编写了这个方法吗

@Chen-jj
Copy link
Contributor

Chen-jj commented Dec 10, 2019

#5012

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

3 participants