简体   繁体   English

Ionic React useState 值在 prop 函数中使用后消失

[英]Ionic React useState value dissapears after using in a prop function

so my usestate hook prints nothing after using it in a parent , im not sure about importing types in typescript since i just move from javascript.所以我的 usestate 钩子在父级中使用它后什么也不打印,我不确定在打字稿中导入类型,因为我只是从 javascript 移动。

    interface Props {
    sendTextMessage: (text?: string) => void
}

const Chatbar: React.FC<Props> = ({ sendTextMessage }) => {
    const [text, setText] = useState<string>()

    return (

                        <IonInput
                            class='input'
                            value={text}
                            placeholder='Aa'
                            onIonChange={(e) => setText(e.detail.value!)}
                            onKeyPress={(event) => {
                                if (event.key === 'Enter') {
                                    sendTextMessage(text)
                                    setText('')
                                }
                            }}
                            // clearInput
                        ></IonInput>
                
    )

}

the parent function父函数

function sendTextMessage(message) {
    console.log(message)

    const messageEvent = {
        type: 'message',
        timestamp: Date.now(),
        source: {
            type: room?.get('type'),
            conId: room?.get('id'),
            userId: 'me',
        },
        message: {
            type: 'text',
            text: message,
        },
    }

<Chatbar sendTextMessage={sendTextMessage} />

i have also tried using another state in the parent file and that did nothing!!!我也试过在父文件中使用另一个状态,但什么也没做!!! i have no idea what i am doing wrong我不知道我做错了什么

SOLVED解决了

node modules was corrupted for some reason, never really had that happend to me before, reinstalling rect works!由于某种原因,节点模块已损坏,我以前从未真正发生过这种情况,重新安装 rect 有效! probably my mistake edting the files inside by accident or somthing可能是我无意中编辑了里面的文件或其他错误

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

相关问题 在 useState 设置钩子杀死钩子后调用 React 道具 function - React prop function called after useState set hook kills hook 如何在反应 typescript (useState([]), useState(boolean)) 中传递尽可能多的 function - How to pass function as many prop in react typescript (useState([]), useState(boolean)) React - 使用函数评估道具值时出错 - React - Error using function to evaluate prop value 如何在 react js/react native 中使用 function 发送道具值 - How to send prop value using function in react js/react native 点击两次查看值 - UseState - UseEffect React using Function Component - Clicking twice to see the value - UseState - UseEffect React using Function Component 使用 contextAPI 和 React 使用异步 function 设置 useState 的初始值 - Setting the initial value of useState with async function using contextAPI and React 在 useState 钩子中使用 prop 值会给出未定义的值 - using prop value in useState hook gives undefined value 如何使用函数设置 React 组件 prop 的值? - How to set the value of a React component prop using a function? 发送prop,然后将其用作React中的函数? - Sending prop and then using it as a function in React? React UseState 在渲染后解析当前值 - React UseState resolve current value after rendering
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM