组件根据表单处理来分类,也可以分为两类: 受控组件 非受控组件
首先我们先来安装一个谷歌插件:
找到项目中的 node-modules 里面的 pmmmwh 里面的client 的 ReactRefreshEntry.js 文件 的第八行注释掉
这样 就可以用了
受控组件
其值受到react控制的表单元素 react将state与表单元素的value绑定到一起
index.js代码如下
```javascript
import React from "react";
import ReactDOM from "react-dom";
class App extends React.Component {
state = {
txt: 'hello',
content: '',
city: 'sh',
isChecked:true
}
handleChange = (e)=>{
this.setState({
txt: e.target.value
})
}
handleContent = (e)=>{
this.setState({
content: e.target.value
})
}
handleCity = (e)=>{
this.setState({
city: e.target.value
})
}
handleChecked = (e)=>{
this.setState({
isChecked: e.target.checked
})
}
render() {
return (
<div>
<input type="text" name="txt" value={this.state.txt} onChange={this.handleChange}/>
<textarea name="content" value={this.state.content} onChange={this.handleContent}/>
<select name="city" value={this.state.city} onChange={this.handleCity}>
<option value="bj">beijing</option>
<option value="sh">shanghai</option>
</select>
<input name="isChecked" type="checkbox" checked={this.state.isChecked} onChange={this.handleChecked}/>
</div>
)
}
}
ReactDOM.render(<App />,document.getElementById('root'));
优化此代码,我们可以用一个方法: 可以用name来区别
index.js代码如下:
import React from "react";
import ReactDOM from "react-dom";
class App extends React.Component {
state = {
txt: 'hello',
content: '',
city: 'sh',
isChecked: true
}
handleForm = e => {
const target = e.target;
const { name, type } = e.target;
const v = type === 'checkbox' ? target.checked : target.value;
this.setState({
[name]: v
})
}
render() {
return (
<div>
<input type="text" name="txt" value={this.state.txt} onChange={this.handleForm} />
<textarea name="content" value={this.state.content} onChange={this.handleForm} />
<select name="city" value={this.state.city} onChange={this.handleForm}>
<option value="bj">beijing</option>
<option value="sh">shanghai</option>
</select>
</div>
)
}
}
ReactDOM.render(<App />, document.getElementById('root'));
非受控组件:
就是通过操作DOM的方式来 操作表单 类似于vue里面的ref一样的
index.js代码如下
import React from "react";
import ReactDOM from "react-dom";
class App extends React.Component {
constructor() {
super();
this.txtRef = React.createRef();
}
getTxt = () => {
console.log('文本框的值', this.txtRef.current.value);
}
render() {
return (
<div>
<input type="text" ref={this.txtRef} />
<button onClick={this.getTxt}>获取文本框的值</button>
</div>
)
}
}
ReactDOM.render(<App />, document.getElementById('root'));
根据前两天的学习已经可以写一个购物车了:
只写了功能,没有写样式,超级无敌丑!
index.js代码如下
import React from "react";
import ReactDOM from "react-dom";
import Money from "./Money";
ReactDOM.render(<Money />, document.getElementById('root'));
Money.js代码如下
import React from 'react'
class Money extends React.Component {
state = {
goods: [
{
id: 1,
name: '笔记本',
price: 5999,
count: 1,
total: 5600
},
{
id: 2,
name: '手机',
price: 4200,
count: 1,
total: 4200
},
{
id: 3,
name: '电视机',
price: 6888,
count: 1,
total: 6888
}
]
}
add = (e) => {
this.state.goods.map(item => {
if (item.id == e.target.dataset.id) {
item.count++;
item.total = item.count * item.price;
}
});
this.setState({});
}
sub = (e) => {
this.state.goods.map(item => {
if (item.count >= 1) {
if (item.id == e.target.dataset.id) {
item.count--;
item.total = item.count * item.price;
}
}
});
this.setState({});
}
render() {
return (
<ul>
{
this.state.goods.map(item => {
return <li key={item.id}>
{}
<span>{item.name}:</span>
{}
<span>¥{item.price}</span>
<div id='btn'>
{}
<button onClick={this.add} data-id={item.id}>+</button>
{}
<button>{item.count}</button>
{}
<button onClick={this.sub} data-id={item.id}>-</button>
{}
</div>
<span>总价:{item.total}</span>
</li>
})
}
</ul>
)
}
}
export default Money
|