方式1:hooks实现
import React ,{useState} from 'react'
export default function App() {
let [val,setVal]=useState("123");
return (
<div>
<input value={val} onChange={(e)=>setVal(e.target.value)}/>
</div>
)
}
方法2:常规实现
import React, { Component } from "react";
export default class App extends Component {
state = { val: "国家" };
fun1 = (e) => {
this.setState({
val: e.target.value,
});
};
render() {
let { list } = this.state;
return (
<div>
<input
type="text"
placeholder="评论"
value={this.state.val}
onChange={(e) => {
this.fun1(e);
}}
/>
</div>
);
}
}