React JSX中的不确定复选框 [英] indeterminate checkbox in React JSX

查看:123
本文介绍了React JSX中的不确定复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在JSX中呈现不确定的复选框?

How do I render an indeterminate checkbox in JSX?

以下是我尝试的内容:

render() {
  return <input type="checkbox" 
                checked={this.props.state === "checked"} 
                indeterminate={this.props.state === "indeterminate"} />
}

然而,不确定不是 HTMLElement 的属性,而是属性。如何从React / JSX设置属性?

However, indeterminate is not an attribute on the HTMLElement, but a property. How do I set properties from React / JSX?

推荐答案

您可以使用 componentDidMount step(在初始渲染后调用)来设置该属性:

You can use the componentDidMount step (which is invoked after the initial rendering) to set that property:

componentDidMount() {
    React.findDOMNode(this).indeterminate = this.props.state === "indeterminate";
}

如果您希望使用后续渲染更新该属性,请执行相同的操作在 componentDidUpdate 中也是。

If you want that property to be updated with subsequent renders, do the same thing in componentDidUpdate also.

这篇关于React JSX中的不确定复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆