简体   繁体   English

反应本机 TextInput 以仅显示 state 值

[英]React Native TextInput to show only state values

I'm trying to display only the state values within TextInput but that does not seem to be the case here.我试图在 TextInput 中仅显示 state 值,但这里似乎并非如此。 I have something like the following我有类似以下的东西

const component = () => {
    const [input, updateInput] = useState('');
    return (
        <TextInput
            value={input}
            onChangeText={text => {
                if (validate(text)) {
                    updateInput(text);
                }
            }}
        />
    )
}

If the validate function returns false the state is not updated.如果验证 function 返回 false,则 state 不会更新。 But the value entered in the TextInput field is still visible.但是在 TextInput 字段中输入的值仍然可见。 I'm not sure if this is the expected behavior but I'd like for TextInput to only display the state value.我不确定这是否是预期的行为,但我希望 TextInput 仅显示 state 值。

Update:更新:

Everyone suggested setting state to '' or initial when validation fails.每个人都建议在验证失败时将 state 设置为 '' 或初始值。 This does work in the example above so thanks for that.这在上面的示例中确实有效,因此感谢您。 Unfortunately the actual component I'm working on does not set initial input to empty string ('') like my example.不幸的是,我正在处理的实际组件没有像我的示例那样将初始输入设置为空字符串 ('')。 It works with undefined/null values at the start.它在开始时使用未定义/空值。 I tried setting state in the else section but it didn't work there.我尝试在 else 部分设置 state 但它在那里不起作用。

const component = () => {
    const [input, updateInput] = useState();
    return (
        <TextInput
            value={input}
            onChangeText={text => {
                if (validate(text)) {
                    updateInput(text);
                } else updateInput(input); // This does not seem to work.
            }}
        />
    )
}

Apologies for not using the correct example.很抱歉没有使用正确的例子。

As your state is not updated when validation failed. 由于您的 state 在验证失败时不会更新。 SO just add else condition when validation failed set text to state input value 所以当验证失败时添加其他条件将文本设置为state输入值
const component = () => { const [input, updateInput] = useState(''); return ( <TextInput value={input} onChangeText={text => { if (validate(text)) { updateInput(text); }else{ updateInput(input) } }} /> ) }

I don't know if i fully understood the question here.我不知道我是否完全理解这里的问题。 But don't you need to update your state also when validate returns false to clear the TextField.但是,当验证返回 false 以清除 TextField 时,您是否也不需要更新 state。

<TextInput
    value={input}
    onChangeText={text => {
        updateInput(validate(text) ? text : '');
    }}
/>

If validate returns false and you want this to be reflected by an empty string in the TextInput component, then you could do something like this:如果 validate 返回false并且您希望通过TextInput组件中的空字符串反映这一点,那么您可以执行以下操作:

const component = () => {
    const [input, updateInput] = useState('');
    return (
        <TextInput
            value={input}
            onChangeText={text => {
                if (validate(text)) {
                    updateInput(text);
                } else {
                    updateInput('');
                }
            }}
        />
    )
}

Just replace the '' with what you want to show when the text is not valid.只需将''替换为文本无效时要显示的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM