const element = ( <div> <h1>Hello!</h1> <h2>Good to see you here.</h2> </div> ); // 咱們將JSX分割成多行。咱們推薦使用括號將 JSX 包裹起來 // JSX 的基本語法規則:遇到 HTML 標籤(以 < 開頭),就用 HTML 規則解析;遇到代碼塊(以 { 開頭),就用 JavaScript 規則解析。 // 只有一個頂層標籤 // 標籤閉合
const element = React.createElement( 'h1', {className: 'greeting'}, 'Hello, world!' );
// 注意: 這是簡化的結構 const element = { type: 'h1', props: { className: 'greeting', children: 'Hello, world' } };
const ele = <div tabIndex = "0"></div> // 使用駝峯屬性名約定,而不是html屬性名稱。class => className tabindex => tabIndex
const ele = <div tabIndex = {user.url}></div>
style={ {color: "red"} }
function Welcome(props) { return <h1>Hello, {props.name}</h1>; } const element = <Welcome name="Sara" />; ReactDOM.render( element, document.getElementById('root') );// React DOM 會將元素及其子元素與以前版本逐一對比, 並只對有必要更新的 DOM 進行更新, 以達到 DOM 所需的狀態。