基本上,JSX 单纯只是 React.createElement(component, props, ...children) function 的一个语法糖。
记得在 什麽是 JSX 有提到过 JSX 会经过 Babel 编译。以下是关於 React 更多的细节。
ReactDOM.render(
React.createElement("h1", null, "Hello Bable"),
document.getElementById('root')
);
使用 <XXX.YYY></XXX.YYY>
component 写法
import React from 'react';
const MyComponents = {
CoverImg: function CoverImg(props) {
return <img src={props.src} alt="Girl in a jacket" width="500" height="600">;
}
}
function BlueDatePicker() {
return <MyComponents.CoverImg src="https://reurl.cc/NZ0kOk" />;
}
<hello></hello> //小写 tag 会被视为 html 标签
<Hello></Hello> //Component 定义应该以
function PhotoStory(props) {
return <h1>Hello Photo, {props.story}</h1>;
}
function VideoStory(props) {
return <h1>Hello Video, {props.story}</h1>;
}
const components = {
photo: PhotoStory,
video: VideoStory
};
function Story(props) {
// 正确!JSX 类型可以是大写字母开头的变数。
const SpecificStory = components[props.storyType];
return <SpecificStory story={props.story} />;
}
const element = (
<div>
<Story storyType="photo" story="Here I am!" />
</div>
);
ReactDOM.render(
element,
document.getElementById('root')
);
<MyCounter figure={1 + 2 + 3 + 4} />
<MyComponent message="hello world" />
<MyComponent message={'hello world'} />
<Door isOpen /> // isOpne 为 true
// 使用 other 这样的预设字去展开内容
const Button = props => {
const { kind, ...other } = props;
const className = kind === "primary" ? "PrimaryButton" : "SecondaryButton";
return <button className={className} {...other} />;
};
以上今天
参考资料:
https://zh-hant.reactjs.org/docs/jsx-in-depth.html
<<: 用React刻自己的投资Dashboard Day24 - styled components
>>: TypeScript | Type 研究心得纪录 1
前言 我之前有写透过 lock or CAS 来防治,Racing condition 问题,但如果...
前言 一开始在设计资料库时常常不确定那个 type 要怎麽设,也不知道什麽是 unsigned in...
Aloha!又是我少女人妻Uerica!话说这次有很多以前的朋友同学们一起参加铁人赛,,不但可以一起...
今天要介绍的是最後这个使用Vue-cli制作的专案,前面我们介绍了这个专案的router和vuex部...
Leetcode #99. Recover Binary Search Tree 简单来说二元树里面...