文章来源:https://www.yuque.com/study365
显然JSX写法更便于页面的编写与维护,且简单、直观。
const root = (
第一个组件内容!
{1 === 1 && 第二个组件内容!}
);
{/* 1、单行注释 */}
- 第一个组件内容!
// 2、行尾注释
- 第二个组件内容!
{/*
3、多行注释
1
2
3
*/}
const MyComponent = () => {
const text = "Hello World!";
return (
{text}
);
};
export default MyComponent;
const MyComponent = () => {
const isFinish = true;
const calc = () => {
const a = 1;
const b = 2;
return a + b;
};
return (
{/*1.运算符表达式*/}
2 + 3 的和为:{2 + 3}
{/*2.三元表达式*/}
家庭作业是否完成:{isFinish ? "是" : "否"}
{/*3.进行函数调用*/}
{calc()}
);
};
export default MyComponent;
const MyComponent = () => {
const title = "我是标题的全部内容!";
const url = "https://www.baidu.com";
const className = "span";
return (
{/* 1、绑定普通属性 */}
我是标题...
百度一下
{/* 2.绑定class */}
我是span标签
我是span标签2
{/* 3.绑定style */}
我的字体颜色是红色
);
};
export default MyComponent;
const MyComponent = () => {
const onClick = () => {
window.alert("您点击了按钮1");
}
return (
onClick={() => {
window.alert("您点击了按钮2");
}}
>
按钮2
);
};
export default MyComponent;
const MyComponent = () => {
const renderTitle = (key) => {
if (key > 1) {
return 我是标题1
}
return 我是标题2
}
return (
{
renderTitle(2)
}
{
2 > 1 ? 我会显示出来 : 我会隐藏
}
{
2 > 1 && 我会显示出来
}
);
};
export default MyComponent;
const MyComponent = () => {
const array = ["1", "2", "3", "4"];
return (
{
array.map(i =>- {i}
)
}
);
};
export default MyComponent;
注意:在渲染是,我们需要给渲染项添加一个key,不然会报错:warning: Each child in a list should have a unique "key" prop.
key和React中的diff算法密切相关。后面章节我们会详情学习。
文章来源:https://www.yuque.com/study365/ongmxc/cnxq9l
留言与评论(共有 0 条评论) “” |