基本的区别是,当它Component
,React 将运行/添加其所有生命周期方法 https://facebook.github.io/react/docs/react-component.html#the-component-lifecycle。当您拥有以下内容时,这会很有用state
在你的组件中。当你使用这个组件时,React 会创建一个React Component Instance
它将添加所有生命周期方法和其他挂钩。
class App extends React.Component{
...
}
在某些情况下,您不会使用state
。在这些情况下,添加所有这些生命周期方法是不必要的。因此,React 为您提供了一种创建组件的方法,该组件将具有render
独自的。它被称为PureComponent
。当您使用它时,无需创建新的Component Instance
因为这里没有生命周期方法。它只是一个可以接受的函数props
并返回 React 元素。
class App extends React.PureComponent{
...
}
希望这可以帮助!
[Update]
什么是Component
and a Component Instance
?
从技术上讲,一个Component
在 React 中是一个class
or a function
.
Example:
class App extends React.Component{
...
}
//stateless component
const App = (props) => {
...
}
当你使用它时component
,它将被实例化,更像是new App()
。但是,React 本身以不同的方式做到这一点。
例如:
render(){
return <App/> //Instance of the component App
}
需要实例,因为每个实例都可以单独执行。实例是原始类的副本。
简单的答案是,components
将是一个Class
and component Instance
将是该类的副本/实例并将用于render
希望这能解释!