当前位置:网站首页>Today's code farmer girl learned notes about event operation and ref attribute, and did the practice of form two-way binding
Today's code farmer girl learned notes about event operation and ref attribute, and did the practice of form two-way binding
2022-07-27 11:10:00 【Dingdingding】
Event operations
In virtual dom Call event in , The first letter of the event name should be capitalized , for example :onClick, The function to be called needs to be changed this Point to , Or define it as an arrow function
class Header extends React.Component{// Son
fun(){
this.props.foo("asd")
}
render(){
return (<div><h1 onClick={this.fun}> Click on the I </h1></div>)
}
}
Component variables state
class App extends React.Component{
constructor(props){
super(props)
// Create variables
this.state={msg:"hello"}
}
// Render the page
render(){// Variable call under component
return (<h1>{this.state.msg}</h1>)
}
}
ReactDOM.render(
(<div>
<App />
</div>),
document.getElementById("app")
)
refs attribute
refs yes react One of the special properties of the following , Can be in virtual dom Bind an attribute value in , Under the function, you can directly operate virtual dom Tags and components , In this way, you can manipulate the label style , Text , Properties, etc , Make it easier to get tags
inpvalue(event){
this.refs.inp.style.backgroundColor="pink"
this.refs.inp.vallue=" good "
}
<input type="text" onChange={this.inpvalue} value={this.state.msg} ref="inp"/>
practice : Make a login interface , There are user name and password boxes on the page
Click button , use react Verify that the user name and password are empty , And give a prompt on the page
<body>
<div id="app"></div>
<script type="text/babel">
class App extends React.Component{
constructor(props){
super(props)
this.state={user:"",pwd:"",up:""}
this.fun1=this.fun1.bind(this)
this.fun2=this.fun2.bind(this)
this.value=this.value.bind(this)
}
fun1(e){
var v=e.target.value
this.setState({user:v})
}
fun2(e){
var v=e.target.value
this.setState({pwd:v})
}
value(){
var a=this.state.user
var b=this.state.pwd
if(a=="" || b==""){
this.setState({up:" User name or password cannot be empty "})
}
else{
this.setState({up:" Login successful "})
}
}
render(){
return (
<div>
<p> user name :<input type="text" onChange={this.fun1} value={this.state.user}/></p>
<p> password :<input type="password" onChange={this.fun2} value={this.state.pwd}/></p>
<p><input type="button" value=" Click on " onClick={this.value}/></p>
{this.state.up}
</div>
)
}
}
ReactDOM.render(
(<div>
<App />
</div>),
document.getElementById("app")
)
</script>
</body>边栏推荐
- Analysis of C language pointer function and function pointer
- 正则form表单判断
- Overview of radar communication integrated waveform design
- No identifier specified for entity solution
- 9 UAV array
- antd table+checkbox 默认值显示
- An article reveals the NFT strategy of traditional game manufacturers such as Ubisoft
- 14 check whether integers and their multiples exist
- JVM judges that the object is dead, and practices verify GC recycling
- Chengying, kangaroo cloud one-stop fully automated operation and maintenance steward, is officially open source
猜你喜欢

Tdengine helps Siemens' lightweight digital solution simicas simplify data processing process

antd table+checkbox 默认值显示

SQL Server2000 database error

tensorflow运行报错解决方法

A verification test of the relationship between iteration number and entropy

SQL Server2000数据库错误

计算重叠积分的第二种方法

推导重叠积分的详细展开式 STO overlap integrals

Antd table+checkbox default value display

The difference between scalar, vector, matrix and tensor in deep learning
随机推荐
Thank you for your likes and attention
Compete for the key battle of stock users and help enterprises build a perfect labeling system - 01 live review
IMG SRC is empty or SRC does not exist, and the picture has a white border
学习笔记-简易服务器实现
对象数组去重
Symmetric encryption and asymmetric encryption
Redis high availability principle
Review and Prospect of encrypted traffic identification based on deep learning
IO流_数据输入输出流的概述和讲解
pyquery 的使用
Tdengine helps Siemens' lightweight digital solution simicas simplify data processing process
15 design movie rental system
Time and power allocation method to ensure fairness in sensor fusion system
Antd table+checkbox default value display
ASP. Net core dependency injection journey: 1. Theoretical concepts
Background color style modification on table hover in antd
Maximized array sum after 13 K negations
11 wrong set
迭代次数和熵之间关系的一个验证试验
Tensorflow notes - basic functions and concepts