javascript - this.handleChange.bind(this) 和渲染使用
问题描述
我从 react bootstrap 网站 https://react-bootstrap.github.io/components/forms/得到了这段代码 我想知道代码中以下内容的使用
- this.handleChange = this.handleChange.bind(this)
- 使
value
in 为this.state = { value: '' }; }
空的使用 - 设置
validationState
属性的使用validationState={this.getValidationState()}
以及设置两个渲染的使用,为什么不只是一个渲染,是否可以设置多个渲染
class FormExample extends React.Component { constructor(props, context) { super(props, context); this.handleChange = this.handleChange.bind(this); this.state = { value: '' }; } getValidationState() { const length = this.state.value.length; if (length > 10) return 'success'; else if (length > 5) return 'warning'; else if (length > 0) return 'error'; return null; } handleChange(e) { this.setState({ value: e.target.value }); } render() { return ( <form> <FormGroup controlId="formBasicText" validationState={this.getValidationState()} > <ControlLabel>Working example with validation</ControlLabel> <FormControl type="text" value={this.state.value} placeholder="Enter text" onChange={this.handleChange} /> <FormControl.Feedback /> <HelpBlock>Validation is based on string length.</HelpBlock> </FormGroup> </form> ); } } render(<FormExample />);
解决方案
- this.handleChange = this.handleChange.bind(this): 这是为了让你可以在 handlechange() 中使用变量“this”
- 通过设置一个空白值,如果您尝试在没有设置值的情况下引用它,该键将来不会为空
- validationState 作为属性传递给 FormGroup
- 渲染不在同一个类中,因此 FormExample 正在由它之外的其他东西渲染。
希望有帮助
推荐阅读
- amazon-web-services - AWS Cognito AdminLinkProviderForUser 尝试更新不可变用户属性
- sorting - DataTables 对没有空格和字符串的数字进行排序
- neo4j - Neo4j 单查询重构往来关系
- python - 绘图。在图表上显示数据,而不是从悬停中获取数据
- asp.net-mvc - Swashbuckle Swagger url 从 http 到 https .NET
- java - WebView 在 android 4.2(17) 中不加载 instagram 登录网站
- javascript - 为什么我们需要复制 useState 对象来更改 UI?
- html - 如何在 3-col 的情况下调整不拆分最后两个项目之间的空间?
- r - 在 R 中使用 gsub() 时出现无效的正则表达式错误
- php - 按国家/地区限制对 WooCommerce 产品的访问