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

fix: locale api is undefined before react app mounted #1810

Merged
merged 1 commit into from
Jan 11, 2019

Conversation

yutingzhao1991
Copy link
Contributor

类似 formatMessage 这样的 API 在 react-intl 在用法是需要通过 context 然后在组件中通过 this.formatMessage 来使用。在 umi-plugin-locale 中为了更方便,通过封装了一层之后使得用户可以直接调用 formatMessage 来使用,但是有一个问题是必须要 react app 挂载之后 formatMessage 才能实际可用。

也就是说如果直接在模块初始化部分使用的话是无效了,最初是有一个空的方法做容错处理,后来在 #1730 这个 PR 中去掉了容错的方法,会导致如果再模块初始化阶段拿到的方法是 undefined。

在这个 PR 中重新加上了容错的方法,并且在模块初始化调用的时候给出警告提示。另外去掉了 #1730 这个 PR 中添加的 locale 这些非方法的内容,如果这些内容需要用到的话建议还是类似提供 getLocale 这样的方法来调用。

@yutingzhao1991
Copy link
Contributor Author

@imhele

@coveralls
Copy link

coveralls commented Jan 11, 2019

Pull Request Test Coverage Report for Build 2447

  • 5 of 10 (50.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.03%) to 30.528%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/umi-plugin-locale/src/locale.js 5 10 50.0%
Totals Coverage Status
Change from base Build 2439: -0.03%
Covered Lines: 1197
Relevant Lines: 4041

💛 - Coveralls

@sorrycc sorrycc merged commit 8e1df12 into master Jan 11, 2019
@sorrycc sorrycc deleted the fix/formatMessage branch January 11, 2019 02:44
@yutingzhao1991 yutingzhao1991 restored the fix/formatMessage branch January 11, 2019 02:53
@imhele
Copy link
Contributor

imhele commented Jan 11, 2019

🤔学习了

@Verten
Copy link

Verten commented Jan 29, 2019

在跑test的时候,组件使用了formatMessage({id:'xxx'}) 这个方法,但是在test的时候出现warning

image

怎么解决?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants