为了进一步阐述这个问题的答案,React Element 没有任何方法,也没有关于原型的任何东西。这也使他们更快。
“ ReactElement 是一个轻量级的、无状态的、不可变的 DOM 元素的实际代表”-反应术语词汇
A react component render() function returns a DOM tree of react elements behind the scenes (This is the virtual DOM btw). There is some complex mapping and diff logic involved, but basically these React elements map to the DOM elements.
您还可以直接创建 Element React.createElement(arg),其中 arg 可以是 html 标记名,也可以是 React Component 类。
A React Element is just a plain old JavaScript Object without own methods. It has essentially four properties:
type,表示 HTML 标记或引用反应组件的 String
key,唯一标识反应元素的 String
ref, a reference to access either the underlying DOM node or React Component Instance)
props(属性 Object)
A React Element is not an instance of a React Component. It is just a simplified "description" of how the React Component Instance (or depending on the type an HTML tag) to be created should look like.
描述 React Component 的 React Element 不知道它最终呈现到哪个 DOM 节点-这种关联是抽象的,并且在呈现时将被解析。
React Elements 可能包含子元素,因此能够形成元素树,这表示 Virtual DOM 树。
A React Component can contain state and has access to the React Lifecycle methods. It must have at least a render method, which returns a React Element(-tree) when invoked. Please note that you never construct React Component Instances yourself but let React create it for you.
This is slightly surprising, since if you're used to other UI frameworks you might expect that there'd only be two kinds of thing, roughly corresponding to classes (like Widget) and instances (like new Widget()). That's not the case in React; component instances are 没有 the same thing as elements, nor is there a one-to-one relationship between them. To illustrate this, consider this code:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
class MyComponent extends React.Component {
constructor(props) {
super(props);
console.log('This is a component instance:', this);
}
render() {
const another_element = <div>Hello, World!</div>;
console.log('This is also an element:', another_element);
return another_element;
}
}
console.log('This is a component:', MyComponent)
const element = <MyComponent/>;
console.log('This is an element:', element);
ReactDOM.render(
element,
document.getElementById('root')
);
Finally, it should be noted that while the official docs are rigorous about using the term "component" to refer to a function or class and "component instance" to refer to an instance, other sources do not necessarily adhere to this terminology; you should expect to see "component" used (incorrectly) to mean "component instance" when reading Stack Overflow answers or discussions on GitHub.
React lets you define UIs as pure functions defined on application state. It could implement this by computing the entire UI during each state change, but this would be expensive. Elements are computational descriptions (thunks), and if they don't change, and you're using PureComponents, React won't bother recomputing that subtree.
class MyComponent extends Component {
render() {
const {myState} = this.state || {};
const message = `The current state is ${myState}.`;
return (
<div>{message}</div>
);
}
}
使用生命周期挂钩
class MyComponent extends Component {
// Executes after the component is rendered for the first time
componentDidMount() {
this.setState({myState: 'Florida'});
}
render() {
const {myState} = this.state || {};
const message = `The current state is ${myState}.`;
return (
<div>{message}</div>
);
}
}
And if we do console.log(<Box />) we say that we are logging a 反应元素 to the console, and same thing with the following code console.log(<Box>...<Box />).