-
在Terminal中输入以下命令
npm i element-react --save
npm install element-theme-default --save
npm install react-hot-loader@next --save
-
右击src新建ClickCount.css文件,设置风格
.ClickCount{
display: flex;
flex-direction: column;
}
.button1{
background-color: white;
width: 130px;
font-family: "Times New Roman", serif;
font-size: 25px;
}
.text{
font-size: 20px;
margin-top: 10px;
}
-
右击src新建ClickCount.js文件,并引入相应的.css文件,代码如下
import React, { Component } from 'react';
import { Button } from 'element-react';
import './ClickCount.css'
class ClickCounter extends Component {
constructor(props) {
super(props);
this.onClickButton = this.onClickButton.bind(this);
this.state = {
count: 0
}
}
onClickButton() {
this.setState({count: this.state.count + 1});
}
render() {
const counterStyle = {
margin: '16px'
}
return (
<div style={counterStyle} className="ClickCount">
<Button className="button1" type='primary' onClick={this.onClickButton}>
Click Me
</Button>
<div className="text">
Click Count: <span id="clickCount">{this.state.count}</span>
</div>
</div>
);
}
}
export default ClickCounter;
-
在index.js里引入element-react,代码如下
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import 'element-react'
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
reportWebVitals();
-
在App.js中引入ClickCount组件,并放置在图片下一行处,代码如下
import logo from './logo.svg';
import './App.css';
import ClickCounter from "./ClickCount";
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<ClickCounter />
<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>
);
}
export default App;