We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
No description provided.
The text was updated successfully, but these errors were encountered:
在react中创建组件的形式有三种
今天我们要聊的是纯函数式定义的无状态组件及类组件的到底有什么不同, 分别在什么场景下适合使用
首先我们来看一下用上述方法如何来创建一个组件
1. Extends React.Component 定义的组件
React.Component是以ES6的形式来创建react的组件的,是React目前极为推荐的创建有状态组件的方式,最终会取代React.createClass形式
class Demo extends React.Component { constructor(props) { super(props); this.state = { text: props.initialValue || 'placeholder' }; // 手动绑定this this.handleChange = this.handleChange.bind(this); } handleChange(event) { this.setState({ text: event.target.value }); } render() { return ( <div> <input value={this.state.text} onChange={this.handleChange}/> </div> ); } } Demo.propTypes = { initialValue: React.PropTypes.string }
2. 纯函数式定义的无状态组件
纯函数组件的特点:
function DemoComponent(props) { return <div>Hello {props.name}</div> } ReactDOM.render(<DemoComponent name="每日一题" />, mountNode)
使用场景
以类形式创建的组件不用多说,该怎么用还怎么用, 这里说一纯函数组件, 纯函数组件被鼓励在大型项目中尽可能以简单的写法来分割原本庞大的组件,未来React也会这种面向无状态组件在譬如无意义的检查和内存分配领域进行一系列优化,所以只要有可能,尽量使用无状态组件
总结
Sorry, something went wrong.
useEffect
React.forwardRef
No branches or pull requests
No description provided.
The text was updated successfully, but these errors were encountered: