【发布时间】:2021-02-24 21:22:47
【问题描述】:
所以我有一个以这种方式使用的组件。
<SBCAInput>
<input defaultValue="" type="email" />
</SBCAInput>
而我在SBCAInput 组件中是这样实现的。
import React , {useState} from 'react'
function SBCAInput(props) {
const [value, setValue] = useState("");
return (
<div className={`sbca-input mb-3 ${ value==="" ? "" : "input-not-empty"}`}>
<label>Email address</label>
{props.children}
</div>
)
}
export default SBCAInput
现在的挑战是我希望能够检测input 的值是否为空并更改SBCAInput 组件中div 容器上的类。
如何跟踪输入组件的值?
【问题讨论】:
-
IMO 检查
useRef钩子:reactjs.org/docs/hooks-reference.html#useref
标签: javascript reactjs react-props