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

Symbol的诡异问题 #3706

Closed
Here21 opened this issue Jul 7, 2019 · 3 comments
Closed

Symbol的诡异问题 #3706

Here21 opened this issue Jul 7, 2019 · 3 comments

Comments

@Here21
Copy link

Here21 commented Jul 7, 2019

问题描述
使用Symbol的时候遇到一个诡异的问题。
问题描述图片:
image

// constant
export const MODE = {
  VIEW: Symbol('VIEW'),
  SELECT_GROUP: Symbol('SELECT_GROUP'),
  SELECT_ROUTINE: Symbol('SELECT_ROUTINE')
}

// default mode is Symbol('VIEW')
...
 const {
      exercisesStore: { exercises, mode }
    } = this.props
    console.log('---', mode, MODE.SELECT_ROUTINE === mode) // 输出: false
...
// 组件会被显示出来,但是仅仅是显示,props没有传进去
// 可以看到上面图片组件的count是0,而且dispatchDone不会触发
 {mode === MODE.SELECT_ROUTINE && (
          <SelectedPanel dispatchDone={this.handleSelectedDone} count={1} />
        )}

而且Mobx中改变属性的Symbol值,会报警告:
image

// mobx state
mode: MODE.VIEW,

// mobx function
  changeState(mode) {
    console.log(mode)
    if (MODE.VIEW === mode) {
      this.mode = MODE.VIEW
    }
    if (MODE.SELECT_GROUP === mode) {
      this.mode = MODE.SELECT_GROUP
    }
    if (MODE.SELECT_ROUTINE === mode) {
      this.mode = MODE.SELECT_ROUTINE
    }
  },

image
但是不使用Symbol时,就正常:

export const MODE = {
  VIEW: 'VIEW',
  SELECT_GROUP: 'SELECT_GROUP',
  SELECT_ROUTINE: 'SELECT_ROUTINE'
}

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

  1. 使用Symbol变量去页面render中去判断组件显示/隐藏
  2. 会出现上图1,图2中的问题
// 问题代码
export const MODE = {
  VIEW: Symbol('VIEW'),
  SELECT_GROUP: Symbol('SELECT_GROUP'),
  SELECT_ROUTINE: Symbol('SELECT_ROUTINE')
}
// 正常
export const MODE = {
  VIEW: 'VIEW',
  SELECT_GROUP: 'SELECT_GROUP',
  SELECT_ROUTINE: 'SELECT_ROUTINE'
}

判断行为确实可以正常判断,但是组件内部显示不正常。

期望行为
期望使用Symbol应该有使用字符串一样的期望

报错信息

没有报错信息,只有诡异的期望

系统信息

Taro v1.2 及以上版本已添加 taro info 命令,方便大家查看系统及依赖信息,运行该命令后将结果贴下面即可。

  • 操作系统: Mac Os 10.14.5
  • 微信基础库: 2.7.3
  • Taro 版本 1.3.4
  • Node.js 版本 v8.11.2
  • 报错平台 [weapp]

补充信息

"dependencies": {
		"@antv/f2": "^3.3.9",
		"@tarojs/async-await": "^1.3.4",
		"@tarojs/components": "1.3.4",
		"@tarojs/mobx": "1.3.4",
		"@tarojs/mobx-h5": "1.3.4",
		"@tarojs/mobx-rn": "1.3.4",
		"@tarojs/rn-runner": "1.3.4",
		"@tarojs/router": "1.3.4",
		"@tarojs/taro": "1.3.4",
		"@tarojs/taro-alipay": "1.3.4",
		"@tarojs/taro-h5": "1.3.4",
		"@tarojs/taro-swan": "1.3.4",
		"@tarojs/taro-tt": "1.3.4",
		"@tarojs/taro-weapp": "1.3.4",
		"mobx": "4.8.0",
		"nerv-devtools": "^1.4.3",
		"nervjs": "^1.4.3",
		"prop-types": "^15.7.2",
		"taro-f2": "^2.1.2",
		"taro-ui": "^2.2.0"
	},

还不是很清楚这个问题出在哪里

@taro-bot
Copy link

taro-bot bot commented Jul 7, 2019

欢迎提交 Issue~

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

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

Good luck and happy coding~

@luckyadam
Copy link
Member

Symbol 应该不能直接使用,需要加 polyfill

@Here21
Copy link
Author

Here21 commented Jul 8, 2019

好的,感谢答复

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