React - 组合 vs 继承
React 有十分强大的组合模式。我们推荐使用组合而非继承来实现组件间的代码重用。
Props 和组合为你提供了清晰而安全地定制组件外观和行为的灵活方式。注意:组件可以接受任意 props,包括基本数据类型,React 元素以及函数。
如果你想要在组件间复用非 UI 的功能,我们建议将其提取为一个单独的 JavaScript 模块,如函数、对象或者类。组件可以直接引入(import)而无需通过 extend 继承它们。
function FancyBorder(props) { return ( <div className={'FancyBorder FancyBorder-' + props.color}> {props.children} </div> ); } function Dialog(props) { return ( <FancyBorder color="blue"> <h1 className="Dialog-title"> {props.title} </h1> <p className="Dialog-message"> {props.message} </p> {props.children} </FancyBorder> ); } class SignUpDialog extends React.Component { constructor(props) { super(props); this.handleChange = this.handleChange.bind(this); this.handleSignUp = this.handleSignUp.bind(this); this.state = {login: ''}; } render() { return ( <Dialog title="Mars Exploration Program" message="How should we refer to you?"> <input value={this.state.login} onChange={this.handleChange} /> <button onClick={this.handleSignUp}> Sign Me Up! </button> </Dialog> ); } handleChange(e) { this.setState({login: e.target.value}); } handleSignUp() { alert(`Welcome aboard, ${this.state.login}!`); } } ReactDOM.render( <SignUpDialog />, document.getElementById('root') );
Sep 08, 2021 02:56:54 PM
Do you want to learn some new tricks of magic?
<a href="https://escortservicesindifferentplace.weebly.com/my-work.html">Chennai Escorts </a>
<a href="https://lucknowcallgirldivyanka.hatenablog.com/entry/2021/09/02/185054?_ga=2.256848395.1858775967.1630575313-1424768999.1630575313">Chennai Escort </a>
<a href="https://ext-5523356.livejournal.com/2472.html">Escorts in Chennai </a>
<a href="https://www.evernote.com/shard/s497/client/snv?noteGuid=bbfa3d02-8735-d8ec-d1d8-4bac5cd75b66¬eKey=e3943d3ffdf038af6a3f7d8e61ee8dd3&sn=https%3A%2F%2Fwww.evernote.com%2Fshard%2Fs497%2Fsh%2Fbbfa3d02-8735-d8ec-d1d8-4bac5cd75b66%2Fe3943d3ffdf038af6a3f7d8e61ee8dd3&title=Chennai%2BEscorts"> Chennai Escort Service </a>
Sep 08, 2021 02:57:42 PM
[link:http://www.google.com]your link text[/link]
[link="http://www.google.com"]google[/link]
[url=http://www.google.com]google[/url]
<a href="https://www.google.com">google</a>
[google][http://www.google.com|google ]
[http://www.google.com google ]
[a=http://www.google.com] google[/a]
[google|http://www.google.com]
"google":www.google.com
[google](http://www.google.com/)
[http://www.google.com|google]
[Lighthouse](http://lighthouseapp.com/ "Lighthouse")
Google
[google->http://www.google.com/]