猿问

无法从 ref setNativeProps 更改文本样式

当我的一些 textInput 处于焦点时,我想改变我的组件的一些样式,


我使用世博会 34.0.0


有两种方法,我尝试过:使用 ref 和使用状态,两者都有我需要了解的问题。


到目前为止,<TextInput>当我通过onFocusTextInput 的函数更改它们时,样式按预期工作。


但是当我尝试<Text>通过包含或使用按钮来更改样式时,refTitle通过调用使用相同的方法进行更改refTitle.current.setNativeProps()不起作用,每次我尝试更改文本样式时,它只会返回 refTitle.current.setNativeProps 未定义。


const refTitle = useRef(null)

const refUser = useRef(null)


return (

    <Card>

        <Margin>

            <Text ref={ refTitle } style={ styles.title }>as</Text>

            <TextInput

                ref={ refUser }

                style={ [styles.input] }

                onSubmitEditing={ () => refPassword.current.focus() }

                onFocus={ () => refUser.current.setNativeProps({ style: { borderColor: FOCUS_COLOR } }) }

                onBlur={ () => refUser.current.setNativeProps({ style: { borderColor: BLUR_COLOR } }) }

            />

因此,如果我将 textInput 中的 onFocus 更改为


const handleFocus = () => {

        refTitle.current.setNativeProps({ style: { color: FOCUS_COLOR } })

        refUser.current.setNativeProps({ style: { borderColor: FOCUS_COLOR } })

    }

我不知道为什么它不起作用,一直在搜索但指南告诉我没关系,这是最近 React Native 中的错误吗?


第二。我确实使用了状态,这更简单,但有一种我不明白的错误。


我只是使用 onFocus 函数来设置新颜色,......每次我点击 textInput 时,它都会改变颜色......但它并没有真正聚焦,我必须再次单击以使其完全聚焦(键盘显示和指示器开始滴答作响)


qq_遁去的一_1
浏览 139回答 2
2回答

心有法竹

您必须使用引用才能使用 setNativeProps&nbsp;<TextInput&nbsp;ref={ref => {this.referencedeTF = ref;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }}/>&nbsp; this.referencedeTF.setNativeProps({&nbsp; &nbsp; &nbsp; &nbsp; borderColor: "red",&nbsp; &nbsp; &nbsp; &nbsp; borderWidth: 1&nbsp; &nbsp; &nbsp; });您正在以错误的方式使用参考。

扬帆大鱼

原来我使用的是不同的<Text>而不是来自本机的...解决了它,但我仍然感到困惑,如果使用状态强迫我点击两次以触发焦点,而第一次按下已经改变了颜色,也许是因为文本不是本机的来自本机
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答