亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

React 本機 setState 鉤子重新打開模式

React 本機 setState 鉤子重新打開模式

呼如林 2022-05-22 10:31:24
我對本機反應(博覽會)中的模態有一個奇怪的問題。我的模態看起來像這樣:const useNewCommentModal = () => {    const [showModal, setShowModal] = useState(false);    const [comment, setComment] = useState('');    const toggle = () => {        setShowModal(!showModal);    };    const NewCommentModal = () => (        <Modal visible={showModal} animationType="slide">            <View style={[t.pX4]}>                <TextInput                    style={[t.bgWhite, t.p2, t.rounded, t.textLg]}                    placeholder={'Post jouw reactie...'}                    onChangeText={text => setComment(text)}                    value={comment}                />            </View>        </Modal>    );    return [toggle, NewCommentModal];};export default useNewCommentModal;當我鍵入時,模態不斷重新打開。當我刪除它時:onChangeText={text => setComment(text)}問題消失了,但顯然狀態不再更新。為什么模型不斷重新打開?- 編輯 - const [toggleModal, NewCommentModal] = useNewCommentModal(        'user',        route.params.user.id    );<NewCommentModal />
查看完整描述

1 回答

?
眼眸繁星

TA貢獻1873條經驗 獲得超9個贊

每次你的 useNewCommentModal 鉤子運行時,它都會創建一個名為 NewCommentModal 的新函數,然后你將其用作組件<NewCommentModal />(這部分非常重要)。對于 React,每一個新NewCommentModal的都不同于以前的(因為你每次和每次渲染都創建新函數),React 會在新舊之間運行相等比較,NewCommentModal這將返回 false,因此 React 將卸載舊模式并安裝一個新模式這是地方。發生這種情況是因為您將函數稱為組件。因此,您不應該NewCommentModal從您的鉤子中返回組件,而是返回一個將呈現某些內容的函數 ( renderNewCommentModal),并且不將其作為組件調用,而是作為函數 ( {renderNewCommentModal()}) 調用?;蛘吒玫氖?,只從鉤子返回數據,并使用這些數據在你的主組件中渲染一些東西



查看完整回答
反對 回復 2022-05-22
  • 1 回答
  • 0 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號