Commit 5571d4e4 authored by 郭学岭's avatar 郭学岭

react入门,hook写法

parent 9b0ccdd7
Pipeline #530 failed with stages
SKIP_PREFLIGHT_CHECK=true
\ No newline at end of file
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
react 项目入门
使用之前请安装好react 开发脚手架create-react-app;
### `npm install create-react-app -g`
拉项目到本地,进入项目目录
hook 基本用法请参考src目录下App.js
### `yarn install`
然后选择执行以下命令选择对应操作;
## Available Scripts
In the project directory, you can run:
进入目录,启动项目
### `yarn start`
Runs the app in the development mode.<br />
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
浏览器输入http://localhost:3000 本地访问
The page will reload if you make edits.<br />
You will also see any lint errors in the console.
### `yarn test`
Launches the test runner in the interactive watch mode.<br />
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
项目构建
### `yarn build`
Builds the app for production to the `build` folder.<br />
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.<br />
Your app is ready to be deployed!
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
### `yarn eject`
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
## Learn More
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
To learn React, check out the [React documentation](https://reactjs.org/).
### Code Splitting
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
### Analyzing the Bundle Size
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
### Making a Progressive Web App
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
### Advanced Configuration
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
### Deployment
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
### `yarn build` fails to minify
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
This source diff could not be displayed because it is too large. You can view the blob instead.
.App {
text-align: center;
}
.App-logo {
height: 40vmin;
pointer-events: none;
text-align: left;
font-size: 18px;
}
@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
button {
display: inline;
border: 1px solid gray;
text-align: center;
border-radius: 5px;
outline: none;
margin-left: 5px;
}
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
#txt {
position: absolute;
top: 140px;
left: 10px;
animation: flut 2500ms infinite alternate;
}
.App-link {
color: #61dafb;
#txt em{
color:black;
font-weight: bold;
padding-left:10px;
}
@keyframes App-logo-spin {
@keyframes flut {
from {
transform: rotate(0deg);
left: 0;
}
to {
transform: rotate(360deg);
left: 50px;
color: rosybrown;
}
}
\ No newline at end of file
import React from 'react';
import logo from './logo.svg';
import './App.css';
//react hook 基本用法
import React,{useState, useEffect, useMemo, useCallback} from 'react';//引用react
import Child from './Child';//子组件引用
import './App.css'; //样式的引用
function App() {
return (
function App() { //在hook中模块是以函数的方式存在
const [count,setCount]=useState(0); //使用状态钩子
const [user,setInfor]=useState({name:'tom'});
// useState状态钩子
let clickme = () => {
setCount(count-1);
}
// useEffect 副作用钩子
useEffect(()=>{
console.log('父页面被重新渲染!');
},[user])
// useMemo钩子
let memo= useMemo(()=>{
let result = Math.random() * count;
return result;
},[count])
//useCallback钩子
let getCount= useCallback(() => {
return "count=" + count;
}, [count]);
// 比较 useMemo和useCallback的差异
useEffect(()=>{
console.log(memo,'**')
console.log(getCount,'..')
},[getCount])
return ( //所有页面结构用return 方式返回给使用的模块
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
<div>
{/* 数据双向绑定 */}
<span>当前值:{count} </span>
{/* 箭头函数方法触发事件 */}
<button onClick={()=>setCount(count+1)}>点我+1</button>
{/* 触发自定义方法 */}
<button onClick={clickme}>点我-1</button>
</div>
<div>
<span>名字:{user.name} </span>
<button onClick={()=>setInfor({name:'张三'})}>点我改名</button>
</div>
{/* 利用useMemo返回值定义的变量 */}
<span>useMemo钩子:{memo} </span>
<br></br>
{/* 利用useCallback返回值定义的变量 */}
<span>useCallback钩子:{getCount()}</span>
<Child message={count}/>
</div>
);
}
......
import React, { useMemo, useEffect } from 'react';
import logo from './logo.svg';
import './App.css'
import { green, white } from 'color-name';
function Child(props){
const { message } = props; //获取传递过来的props
useEffect(()=>{
console.log('子组件被渲染')
},[message]);
const showMessage =useMemo(()=>{
return message;
},[message])
// 内嵌样式写法,及图片调用
return <div style={{width:"200px",height:"80px",color:"white",position:"relative"}}>
<img src={logo}/>
<span id="txt">这是子组件,你好<em>{showMessage}</em></span>
</div>
}
export default Child;
\ No newline at end of file
......@@ -10,8 +10,4 @@ ReactDOM.render(
</React.StrictMode>,
document.getElementById('root')
);
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment