当前位置:网站首页>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>边栏推荐
- 12 is at least twice the maximum number of other numbers
- How to build a data index system is the most effective. It will take you a quick start from 0 to 1
- 涌现与形态的局部差异和整体差异
- 计算重叠积分的第二种方法
- Why is the data service API the standard configuration of the data midrange when we take the last mile of the data midrange?
- Ten year structure five year life-07 young and vigorous transformation
- pyquery 的使用
- Yum source installation
- [FPGA tutorial case 40] communication case 10 -- Verilog implementation of a simple OFDM system based on FPGA
- SQL Server2000数据库错误
猜你喜欢

tensorflow运行报错解决方法

Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?

熵与形态的非递进现象

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

Influence of black and white pixel distribution on iteration times

Iptables prevent nmap scanning and binlog explanation

Wilderness search --- search iterations

解决 ImportError: cannot import name 'abs' 导入tensorflow报错

Derivation of the detailed expansion sto overlap integrals

Internal and external troubles of digital collection NFT "boring ape" bayc
随机推荐
洛谷 P3052 [USACO12MAR]Cows in a Skyscraper G
【FPGA教程案例40】通信案例10——基于FPGA的简易OFDM系统verilog实现
11 wrong set
Shock simulation of engine mounting system transient modal dynamic analysis and response spectrum analysis
Why is the data service API the standard configuration of the data midrange when we take the last mile of the data midrange?
Time and power allocation method to ensure fairness in sensor fusion system
Learning notes uni app
计算重叠积分的第二种方法
antd中table hover上去的背景色样式修改
Data assets are king. How to analyze the relationship between enterprise digital transformation and data asset management?
Tdengine business ecosystem partner recruitment starts
14 check whether integers and their multiples exist
Play with the cluster configuration center and learn about the Taier console
Redis+caffeine two-level cache enables smooth access speed
Record of a cross domain problem
迭代次数的差异与信息熵
c语言指针函数和函数指针的辨析
Sorry, you guys have something to deal with in the bank recently, which has been delayed
Integrated design of communication perception based on CSI: problems, challenges and Prospects
洛谷P1896 互不侵犯