Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#总结 减少ref的使用 #1

Open
RocketV2 opened this issue May 18, 2018 · 0 comments
Open

#总结 减少ref的使用 #1

RocketV2 opened this issue May 18, 2018 · 0 comments

Comments

@RocketV2
Copy link
Owner

RocketV2 commented May 18, 2018

项目中ref属性的作用

ref主要是用来获取dom对象,尤其是表单元素,如下代码:

<form onClick={this.eventHandler} onKeyDown={this.eventHandler} >
	<input type="text" ref={this.refDom} />
	<button type='submit'>add</button>
</form>

想获取input元素的值,显然不能使用jQuery的方式通过选择器来获取;而ref属性能够将dom对象作为形参传入函数中,此时函数this.refDom的形参就是input对象;

为何减少ref的使用

react采用虚拟DOM的目的是:减少DOM操作;但是ref属性操作的正是实际的DOM,这与react的理念相违背;

如何减少ref的使用

采用受控表单的方式;

<input type="text" value={this.state.getInputVal} onChange={this.getInputVal}  />
@RocketV2 RocketV2 changed the title 减少ref的使用 #总结 减少ref的使用 May 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant