嗨,欢迎来到 React!
我认为您在这里遇到的部分问题并不是真正特定于 React 的,而是与新的 ES2015 module语法有关。在创建 React 类组件时,对于大多数意图和目的,您可以将其React.createClass
视为在功能上等同于class MyComponent extends React.Component
. 一种是使用新的 ES2015 类语法,而另一种是使用 ES2015 之前的语法。
为了学习module,我建议阅读一些关于新module语法的文章以熟悉它。这是一个很好的开始:http : //www.2ality.com/2014/09/es6-modules-final.html。
简而言之,这些只是语法上的差异,但我将尝试进行快速演练:
/**
* This is how you import stuff. In this case you're actually
* importing two things: React itself and just the "Component"
* part from React. Importing the "Component" part by itself makes it
* so that you can do something like:
*
* class MyComponent extends Component ...
*
* instead of...
*
* class MyComponent extends React.Component
*
* Also note the comma below
*/
import React, {Component} from 'react';
/**
* This is a "default" export. That means when you import
* this module you can do so without needing a specific module
* name or brackets, e.g.
*
* import Header from './header';
*
* instead of...
*
* import { Header } from './header';
*/
export default class Header extends Component {
}
/**
* This is a named export. That means you must explicitly
* import "Header" when importing this module, e.g.
*
* import { Header } from './header';
*
* instead of...
*
* import Header from './header';
*/
export const Header = React.createClass({
})
/**
* This is another "default" export, only just with a
* little more shorthand syntax. It'd be functionally
* equivalent to doing:
*
* const MyClass = React.createClass({ ... });
* export default MyClass;
*/
export default React.createClass({
})