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

学习案例study.js #3

Closed
NotCoderJack opened this issue Nov 16, 2017 · 4 comments
Closed

学习案例study.js #3

NotCoderJack opened this issue Nov 16, 2017 · 4 comments

Comments

@NotCoderJack
Copy link

NotCoderJack commented Nov 16, 2017

你好,最近在知乎上看到大神的这个项目,今天试了下学习案例/study,发现其中的createElement.js文件,虽然在study.js中引用了,但是却没用使用,不懂为什么?另外如果没用使用createElement.js,那vnode的生成是怎么做的呢,使用babel转的吗?

麻烦大神给解答一下。

@Foveluy
Copy link
Owner

Foveluy commented Nov 16, 2017

@gapsaras

问的很好,这个是React的一个难点,也是现在人都不怎么说的一个点。

如果facebook没有出jsx的话,我们用的就是createElement.js书写React。

<div className='shit'>
      asdasdasd
</div>

其实会被babel转换成

createElement('div',{className:'shit'},'asdasdasd')

.babelrc中,你会看见

 ["transform-react-jsx", {
      "pragma": "React.createElement" // 默认 pragma 为 React.createElement
    }],

这一句话,这句话就是帮助babel找到createElement函数的地方。

createElement非常重要,也是生成Vnode的地方。

如果喜欢这个项目,给项目一些星星,近期将会有大更新,支持ComponentDidCatch,以及render返回任意的数据类型

@NotCoderJack
Copy link
Author

@215566435 谢谢大神的及时回复,我的理解是:createElement会被babel使用,作为生成vnode的实现方法,相当于babel对外提供的一个可被用户覆盖的方法,不知是不是这样?

@Foveluy
Copy link
Owner

Foveluy commented Nov 16, 2017

@gapsaras 是的,没错。

@Foveluy
Copy link
Owner

Foveluy commented May 10, 2018

修复

@Foveluy Foveluy closed this as completed May 10, 2018
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