赞
踩
create-react-app 项目名称
npx create-react-app react-demo
npx创建临时的 app包,后面react-demo为项目名称npm start || yarn start
test 或者 01-test等项目名称
favicon.ico和index.html
之外的文件都删除掉<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="theme-color" content="#000000" /> <meta name="description" content="Web site created using create-react-app" /> <title>React App</title> </head> <body> <noscript>You need to enable JavaScript to run this app.</noscript> <div id="root"></div> </body> </html>
import './App.css';
function App() {
return (
<div className="App">
app
</div>
);
}
export default App;
index.js
,这是项目的入口文件 import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
// <React.StrictMode>严格模式节点去掉 会影响useEffect的时机
<React.StrictMode>
<App />
</React.StrictMode>
);
yarn start
import { useState } from "react" function App() { // 01:渲染简单变量 // 02:渲染数组 // 03:渲染对象 // 04:渲染三目 // 05:定义事件 并且修改数据更新 const name = '我是ppp' const arr = [{age:10},{age:20},{age:30}] const obj = { sex:'男',age:40} const active = 1 const [count, setCount] = useState(0); function cilckCount(){ console.log('cilckCount'); setCount(count + 1) } return ( <div className="App"> {/* 01:渲染简单变量 */} <div> name - { name } </div> {/* 02:渲染数组 */} <div> arr - { arr.map((item,idx) =>{ return ( <div key={idx}> age- {idx}- { item.age} </div> ) }) } </div> {/* 03:渲染对象 */} <div> obj - { obj.sex } - { obj.age } </div> {/* 04:渲染三目 */} <div> { active === 1 ? '我是1' : '我是0' } </div> {/* 05:定义事件 并且修改数据更新 */} <div> <button onClick={cilckCount}>点击count++</button> 当前count - {count} </div> </div> ); } export default App;
import React, { Component } from 'react'; export default class 类基础语法渲染数组对象三目事件App extends Component { // 01:渲染简单变量 // 02:渲染数组 // 03:渲染对象 // 04:渲染三目 // 05:定义事件 并且修改数据更新 state = { name:'我是ppp', arr : [{age:10},{age:20},{age:30}], obj : { sex:'男',age:40}, active: 1, count: 0 } componentDidMount() {} cilckCount = ()=> { this.setState({ count:this.state.count + 1 }) } render(){ const { name,arr,obj,active,count } = this.state return ( <div> {/* 01:渲染简单变量 */} <div> name - { name } </div> {/* 02:渲染数组 */} <div> arr - { arr.map((item,idx) =>{ return ( <div key={idx}> age- {idx}- { item.age} </div> ) }) } </div> {/* 03:渲染对象 */} <div> obj - { obj.sex } - { obj.age } </div> {/* 04:渲染三目 */} <div> { active === 1 ? '我是1' : '我是0' } </div> {/* 05:定义事件 并且修改数据更新 */} <div> <button onClick={this.cilckCount}>点击count++</button> 当前count - {count} </div> </div> ) } }
import React, { Component } from 'react'; import AppSon from "./appSon"; export default class App extends Component { state = { } componentDidMount() {} render(){ return ( <div> <div> <AppSon left={<div>我是左边</div>} right={<div>我是右边</div>} > <span>我是app传递给son的默认插槽</span> </AppSon> <AppSon></AppSon> </div> </div> ) } }
import React from 'react'; export default function AppSon(props) { console.log('props',props); const { children,left,main,right } = props return ( <div className='content'> <p>appSon 函数组件</p> <div>默认插槽-{children}</div> <div> <p>具名插槽</p> <div style={{display:'flex'}}> <div style={{width:'30%',textAlign:"center"}}>{left ? left : 'left'}</div> <div style={{width:'40%',textAlign:"center"}}>{main ? main : 'main'}</div> <div style={{width:'30%',textAlign:"center"}}>{right ? right : 'right'}</div> </div> </div> </div> ) }
import React, { Component } from 'react'; import AppSon2 from "./appSon2"; export default class App extends Component { state = { } componentDidMount() {} render(){ return ( <div> <div> <AppSon2 left={<div>我是左边-son2</div>} right={<div>我是右边-son2</div>} > <span>我是app传递给son2的默认插槽</span> </AppSon2> <AppSon2></AppSon2> </div> </div> ) } }
import React, { Component } from 'react'; export default class AppSon2 extends Component { state = { } componentDidMount() {} render(){ const { children,left,main,right } = this.props return ( <div> <p>appSon2 类组件</p> <div>默认插槽-{children}</div> <div> ------------分界线 ---------</div> <div> <p>具名插槽</p> <div style={{display:'flex'}}> <div style={{width:'30%',textAlign:"center"}}>{left ? left : 'left'}</div> <div style={{width:'40%',textAlign:"center"}}>{main ? main : 'main'}</div> <div style={{width:'30%',textAlign:"center"}}>{right ? right : 'right'}</div> </div> </div> </div> ) } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。