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

useEffect里使用空return语句编译失败 #3469

Closed
regrex opened this issue Jun 18, 2019 · 3 comments
Closed

useEffect里使用空return语句编译失败 #3469

regrex opened this issue Jun 18, 2019 · 3 comments
Assignees

Comments

@regrex
Copy link

regrex commented Jun 18, 2019

问题描述
[问题描述:站在其它人的角度尽可能清晰地、简洁地把问题描述清楚]
使用useEffect的时候,如果包含空return语句,会编译失败,复现步骤如下

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

  1. 在useEffect里判断依赖是否符合预期,不符合则直接return,啥也不做,如图
    image
  2. 编译失败,并且编译后的return语句非常诡异,变成了return ;,导致报错
    image
  3. 如果改成下面这样,就不会编译失败,但是感觉并不合理,因为提前return的情况很多,此处不应该报错
    image
// 这里可以贴代码
// 点击链接查看如何插入代码:
// https://coding.net/help/doc/project/markdown.html#i-5
  const immerse = false
  const top = 80
  const [opacity, setOpacity] = Taro.useState(1)
  Taro.useEffect(() => {
    if (!immerse) {
      return
    }
    if (!top) {
      return
    }
    const op = top > 120 ? 1 : (top > 40 ? (top - 40) / 100 : 0)
    setOpacity(op)
  }, [immerse, top])
// ```js
// 代码放这里(前后的 "```" 有助 Mardown 整理代码格式和加入代码高亮)
// ```

// 提供完整可复现的代码和整理好代码格式,有助于我们快速定位问题,节省你我时间。
// 代码提供不全或代码格式混乱的 issues 【有可能被忽略】,烦请各位注意。

期望行为
[这里请用简洁清晰的语言描述你期望的行为]
useEffect里可以使用return
报错信息

[这里请贴上你的完整报错截图或文字]

系统信息

Taro v1.2 及以上版本已添加 taro info 命令,方便大家查看系统及依赖信息,运行该命令后将结果贴下面即可。
Taro CLI 1.3.0 environment info:
System:
OS: macOS 10.14.3
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 10.15.3 - ~/.nvm/versions/node/v10.15.3/bin/node
Yarn: 1.12.3 - /usr/local/bin/yarn
npm: 6.4.1 - ~/.nvm/versions/node/v10.15.3/bin/npm
npmPackages:
@tarojs/async-await: ^1.3.0 => 1.3.1
@tarojs/components: 1.3.0 => 1.3.0
@tarojs/plugin-babel: 1.3.0 => 1.3.0
@tarojs/plugin-csso: 1.3.0 => 1.3.0
@tarojs/plugin-less: 1.3.0 => 1.3.0
@tarojs/plugin-uglifyjs: 1.3.0 => 1.3.0
@tarojs/router: 1.3.0 => 1.3.0
@tarojs/taro: 1.3.0 => 1.3.0
@tarojs/taro-alipay: 1.3.0 => 1.3.0
@tarojs/taro-h5: 1.3.0 => 1.3.0
@tarojs/taro-swan: 1.3.0 => 1.3.0
@tarojs/taro-tt: 1.3.0 => 1.3.0
@tarojs/taro-weapp: 1.3.0 => 1.3.0
@tarojs/webpack-runner: 1.3.0 => 1.3.0
eslint-config-taro: 1.3.0 => 1.3.0
eslint-plugin-taro: 1.3.0 => 1.3.0
nervjs: ^1.4.0 => 1.4.0

  • 操作系统: [e.g. Windows 10]
  • Taro 版本 [e.g. v.0.0.64]
  • Node.js 版本 [e.g. v9.0.0]
  • 报错平台 [h5, weapp]

补充信息
[可选]
[根据你的调查研究,出现这个问题的原因可能在哪里?]

@taro-bot
Copy link

taro-bot bot commented Jun 18, 2019

欢迎提交 Issue~

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

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

Good luck and happy coding~

@taro-bot
Copy link

taro-bot bot commented Jun 18, 2019

CC @yuche

@yuche
Copy link
Contributor

yuche commented Jun 18, 2019

你可以先把这个 effect 放到一个自定义 effect 里再掉用

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