当前位置:网站首页>今天的码农女孩学习了关于事件操作和ref属性的笔记并做了表单双向绑定的练习
今天的码农女孩学习了关于事件操作和ref属性的笔记并做了表单双向绑定的练习
2022-07-17 00:06:00 【丁丁丁dingdingding】
事件操作
在虚拟dom中调用事件,事件名首字母要大写,例如:onClick,在调用的函数需要改变this指向,或者定义成箭头函数
class Header extends React.Component{//子
fun(){
this.props.foo("asd")
}
render(){
return (<div><h1 onClick={this.fun}>点击我</h1></div>)
}
}
组件变量state
class App extends React.Component{
constructor(props){
super(props)
//创建变量
this.state={msg:"hello"}
}
//渲染页面
render(){//组件下的变量调用
return (<h1>{this.state.msg}</h1>)
}
}
ReactDOM.render(
(<div>
<App />
</div>),
document.getElementById("app")
)
refs属性
refs是react下的一个特殊属性,可以在虚拟dom里绑定一个属性值,在函数下可以直接操作虚拟dom标签和组件,这样就可以操作标签的样式,文本,属性等内容,让获取标签更简单
inpvalue(event){
this.refs.inp.style.backgroundColor="pink"
this.refs.inp.vallue="好"
}
<input type="text" onChange={this.inpvalue} value={this.state.msg} ref="inp"/>
练习:做一个登录界面,页面上有用户名和密码框
点击按钮,用react验证用户名和密码是否为空,并在页面上给出提示
<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:"用户名或密码不能为空"})
}
else{
this.setState({up:"登录成功"})
}
}
render(){
return (
<div>
<p>用户名:<input type="text" onChange={this.fun1} value={this.state.user}/></p>
<p>密码:<input type="password" onChange={this.fun2} value={this.state.pwd}/></p>
<p><input type="button" value="点击" onClick={this.value}/></p>
{this.state.up}
</div>
)
}
}
ReactDOM.render(
(<div>
<App />
</div>),
document.getElementById("app")
)
</script>
</body>边栏推荐
- 量化行业知识汇总
- 替换URL中的特殊字符(%E2%80%8B)
- 记一次用canvas做出腾讯云首页banner流光效果的经历
- Assemblage stylisé de cartes de commutation auto - encapsulées
- The C Programming Language (2nd)--笔记--1.8
- js截取字符串前几位或者截取字符串后几位
- Record once easy_ SQL Stack Injection
- 数组操作——判断、去重、合并、展开
- uni-app微信小程序——商城(6)——我的主页
- JS intercepts the first few digits of the string or the last few digits of the string
猜你喜欢

微信推送-模版消息参数配置

NodeJS 跨域 CORS

Single page application spa and multi page application MPa

Oracle automatic storage management 18C step-by-step installation -2

uni-app微信小程序——商城(7)——商品详情

自己封裝的風格化的開關卡片組件

自己封装的风格化的开关卡片组件
![Understand PHP from [Fifth space 2021] easycleanup_ session](/img/fc/95332d488dd6096f3a3f6a9fb11644.png)
Understand PHP from [Fifth space 2021] easycleanup_ session

小程序嵌入网页向小程序跳转并传参,微信小程序中实现公众号授权获取openId
![Stack injection [strong net cup 2019] random note](/img/94/9069ed79e994e3c2e96c3cd1f816ee.png)
Stack injection [strong net cup 2019] random note
随机推荐
Summary of XML external entity injection (xxE target recurrence)
鼠标右键菜单添加快速打开选项遇见的错误:
The C Programming Language 2nd --笔记--6.7
网站如何统计访问人数?51LA如何安装使用?
Oracle database architecture
extern和static的区别
@Configurationproperties annotation usage
今天的码农女孩做了关于生命周期的笔记以及动态时钟的练习
(八)Shell函数
The eye of Depth III - - (3)] Mathematics: Matrix eigenvalue and eigenvector 1
替换URL中的特殊字符(%E2%80%8B)
Cve-2022-34265 Django extract & TRUNC SQL injection vulnerability recurrence
网站被黑,通过百度/搜狗等搜索关键词访问跳转到其他网站怎么办?
JS intercepts the first few digits of the string or the last few digits of the string
object-fit:cover; It doesn't work in the applet. How to deal with the deformation of the applet image
js截取字符串前几位或者截取字符串后几位
Maker-HarmonyOS应用开发培训笔记01
[elementui El date picker date selector, the end time must not be earlier than the start time, and only the date of the specified number of days from the start time can be selected]
页面布局——三栏布局解决方式
(七)流程控制