You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native中TextInput无法输入文字问题求助

React Native TextInput输入文字不显示问题解决

你的问题出在TextInput的属性使用错误:你用了defaultValue来绑定状态,但这不符合受控组件的用法。

你通过useState维护了text状态,这属于受控组件场景,此时应该用value属性绑定状态值,而不是defaultValue。defaultValue是给非受控组件设置初始值用的,它只会在组件初始化时生效一次,后续状态更新不会同步到输入框,所以你输入的内容无法显示出来。

修改后的代码如下:

const Login = ({ navigation } : any) => {

    const [text, setText] = useState('');

    return (           
        <View style={[styles.container, { flexDirection: "column" }]}>
            <View style={{ flex: 1, alignItems:"center", justifyContent:"center" }}> 
                <Image source={require('../assets/images/help.png') } style={{ width: 40, height: 40 }} />
            </View>
            <View style={{ flex: 2, backgroundColor: "#5f6364", borderTopRightRadius:20, borderTopLeftRadius:20 }}> 
                <TextInput style={styles.input}  placeholder="Type here to translate!" onChangeText={newText => setText(newText)} value={text} />
            </View>            
        </View>      
    )
}

改成value={text}后,输入框会实时同步text状态的值,你输入内容时通过onChangeText更新状态,就能看到输入的文字正常显示了。

内容的提问来源于stack exchange,提问作者Code Ratchet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 20:35:24