当前位置:网站首页>今天的码农女孩学习了关于事件操作和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>边栏推荐
- 使用bat自动执行cmd命令(多个命令或单个命令)
- Redis简单使用
- 单页面应用 SPA 和多页面应用 MPA
- C Programming Language (2nd Edition)--读书笔记--1.3
- Vue项目部署,清理缓存
- Eye of depth III - (6)] mathematics: matrix diagonalization and quadratic form 1
- Gateway Kong route adding instructions
- Maker-HarmonyOS应用开发培训笔记01
- C Programming Language (2nd Edition)--读书笔记--1.5.3
- 分布式之数据库和缓存双写一致性方案解析(转载)
猜你喜欢
随机推荐
wget 警告: 无法验证
If the website is hacked, what if you jump to other websites through Baidu / Sogou and other search keywords?
jsx 编译
uni canvas截取图片
深度之眼三——(4,5)】数学:矩阵特征值与特征向量2
小程序嵌入网页向小程序跳转并传参,微信小程序中实现公众号授权获取openId
Codeforces round #664C
C Programming Language(2nd Edition)--读书笔记--1.5.1
Record buuctf [netding Cup 2018] unfinish1 problem solving ideas
Oracle automatic storage management (ASM)
promise
Moveit2——1.开始
Add, delete, modify and check the connection between the two tables
“我的”Bug大全(转载)
Maker-鸿蒙应用开发培训笔记03
Day16 sorting, current limiting
学习STM32F103时涉及的C语言知识汇总(仅链接)
(五)printf(代替echo)
uni-app微信公众号(5)——新增、修改地址
uni-app微信小程序——商城(6)——我的主页









