nvm -v
nvm install 16.20.2
nvm use 16.20.2
nvm install -g create-react-app
create-react-app redux_test
<!DOCTYPE html>
<html><head><meta charset="UTF-8"><title>redux</title></head><body><div id="root"></div></body>
</html>
import React, {Component} from 'react';
import Count from "./components/Count";class App extends Component {render() {return (<div><Count/></div>);}
}export default App;
import React from "react";
import ReactDOM from 'react-dom'
import App from './App'ReactDOM.render(<App/>,document.getElementById('root'))
- src/components/Count/index.jsx
import React, {Component} from 'react';class Count extends Component {state = {count:0}// 加法increment=()=>{const {value} = this.selectNumberconst {count} = this.statethis.setState({count:count+value*1})}// 减法decrement=()=>{const {value} = this.selectNumberconst {count} = this.statethis.setState({count:count-value*1})}// 奇数再加incrementIfOdd=()=>{const {value} = this.selectNumberconst {count} = this.stateif(count % 2 !== 0) {this.setState({count:count+value*1})}}// 异步加incrementAsync=()=>{const {value} = this.selectNumberconst {count} = this.statesetTimeout(()=>{this.setState({count:count+value*1})},500)}render() {return (<div><h1>当前求和为:{this.state.count}</h1><select ref={c => this.selectNumber = c}><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> <button onClick={this.increment}>+</button> <button onClick={this.decrement}>-</button> <button onClick={this.incrementIfOdd}>当前求和为奇数再加</button> <button onClick={this.incrementAsync}>异步加</button> </div>);}
}export default Count;