create-react-app
零配置快速构建项目
官方文档地址:https://www.html.cn/create-react-app/docs/getting-started/
创建项⽬: npx create-react-app my-app
打开项⽬: cd my-app
启动项⽬: npm run start
暴露配置项: npm run eject
JSX基本语法
// 修改index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
// import App from './App';
import reportWebVitals from './reportWebVitals';
// 变量
const text = "hello";
// 对象
const obj = {
name: "josie",
age: 99,
}
// 函数
const formatInfo = (info)=>(
`name:${info.name},age:${info.age}`
)
// jsx对象
const msg = <div>msg</div>;
let show = false;
// 数组
const arr = ["a", "b", "c"];
const jsx = <div>
{/* 变量 */}
<div>{text}</div>
{/* 对象 */}
<div>name:{obj.name}</div>
{/* 函数 */}
<div>{formatInfo(obj)}</div>
{/* jsx对象 */}
{msg}
{/* 条件语句 */}
{
show ? 'show': 'none'
}
<ul>
{arr.map(item => (
<li key={item}>{item}</li>
))}
</ul>
</div>
ReactDOM.render(
jsx,
document.getElementById('root')
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
效果如图