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

React Native运行代码报类型转换表达式需加括号错误,useState要加括号吗?

不需要给useState额外包裹括号来解决这个问题。

错误原因

报错来自你调用useState时的传参写法错误:

  • JavaScript 函数调用时不需要指定形参名称,你在传参时额外加了initalState:的标识,这个非法写法被JS解析器识别为不合法的类型转换表达式,才触发了你看到的报错。

修正方案

直接删除useState调用中的initalState:,仅传入初始状态值即可,修正后的完整代码如下:

import React, {useState} from 'react';
import {View, TextInput, SafeAreaView} from 'react-native';
import styles from './styles';

const DestinationSearch = (props) => {
    const [fromText, setFromText] = useState('');
    const [destinationText, setTextDestinationText] = useState('');

    return (
    <SafeAreaView>
        <View>
            <TextInput 
            value={fromText}
            onChangeText={setFromText}
            style={styles.textInput}
            placeholder="From"
            />
            <TextInput 
            value={destinationText}
            onChangeText={setTextDestinationText}
            style={styles.textInput} 
            placeholder="Where to?"
            />
        </View>
    </SafeAreaView>
  );
};

export default DestinationSearch;

内容的提问来源于stack exchange,提问作者Neetesh Khanal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 09:15:01