react 二次确认框
I've fallen in love with React.js and JSX over the years; state-based rendering and a logical workflow have made me see the light of this modern framework. That doesn't mean I don't sometimes get a bit frustrated that the "simple" things seem harder than they should be. Getting a reference to an element and modifying its properties used to be simple but now you have to take into account you don't usually have element references -- you need to think in a different way. I learned this when I needed to set a checkbox's inderterminate
property, a property not recognized via an attribute, one that requires a handle on the element and setting a property directly.
这些年来,我爱上了React.js和JSX; 基于状态的渲染和合理的工作流程使我了解了这个现代框架。 这并不意味着我有时不会对“简单”的事情看起来比他们应该做的事情感到沮丧。 获取元素的引用并修改其属性以前很简单,但是现在您必须考虑到通常没有元素引用-您需要以不同的方式思考。 当我需要设置复选框的inderterminate
属性时,我就学到了这一点,该属性无法通过属性识别,而该属性需要元素上的句柄并直接设置属性。
To add the indeterminate
property to the checkbox, I needed to take advantage of the ref
attribute:
要将indeterminate
属性添加到复选框,我需要利用ref
属性:
const { value, checked, indeterminate } = this.propsreturn render( el && (el.indeterminate = indeterminate)} />)
Since the ref
is run on each render, the indeterminate
property is updated appropriately, and thus the checkbox appears as expected.
由于ref
在每个渲染器上运行,因此indeterminate
属性会适当更新,因此该复选框将按预期显示。
Regardless of how amazing your framework appears, there's always a blind spot that requires a bit of a hack to accomplish what's expected. That's what a framework does, though: gives you 99% of what you need and makes the 1% difficult!
无论您的框架看起来有多神奇,总会有一个盲点,需要一点技巧才能完成预期的工作。 不过,这就是框架所做的事情:为您提供99%的所需内容,使1%的工作变得困难!
翻译自:
react 二次确认框