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
[react] 如何给非控组件设置默认的值? #894
Labels
react
react
Comments
|
<input name="username" type="text" value={this.state.username} onChange={this.handleChange} />
<input name="username" type="text" ref={username=>this.username=username}/> |
给组件设置默认值,难道就没人说static defaultProps吗? |
可在state设置默认值; |
给非受控组件设置defaultValue属性,给定默认值 示例代码: import React from 'react'
export default class UnControlledComp extends React.Component {
constructor(props) {
super(props)
this.inputRef = React.createRef()
}
handleClick = () => {
console.log(this.inputRef.current.value);
}
render () {
return (
<>
<input defaultValue="default value" ref={this.inputRef}></input>
<button onClick={this.handleClick}>点击</button>
</>
)
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[react] 如何给非控组件设置默认的值?
The text was updated successfully, but these errors were encountered: