第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

React 使用 Typescript 創(chuàng)建動態(tài)引用

React 使用 Typescript 創(chuàng)建動態(tài)引用

繁星點點滴滴 2021-10-14 11:04:21
我有一個table列表,其中每一行都有一個menu按鈕,為此我需要一個ref. 我在我的項目中使用 react mui,它是menu。我試過像這樣創(chuàng)建參考:const {rows} = props;const refs = Array.from({length: rows.length}, a => React.useRef<HTMLButtonElement>(null));然后嘗試map在每個函數(shù)上使用像這樣的內(nèi)部函數(shù)button:<Button   ref={refs[index]}   aria-controls="menu-list-grow"   aria-haspopup="true"   onClick={() => handleToggle(row.id)}>Velg</Button>  <Popper open={!!checkIfOpen(row.id)} anchorEl={refs[index].current} keepMounted transition disablePortal>    {({TransitionProps, placement}) => (       <Grow        {...TransitionProps}        style={{transformOrigin: placement === 'bottom' ? 'center top' : 'center bottom'}}>       <Paper id="menu-list-grow">         <ClickAwayListener onClickAway={(e) => handleClose(e, refs[index].current)}>          <MenuList>                                                                    <MenuItem             onClick={(e) => handleClose(e, refs[index].current)}>Profile</MenuItem>           <MenuItem onClick={(e) => handleClose(e, refs[index].current)}>My account</MenuItem>           <MenuItem onClick={(e) => handleClose(e, refs[index].current)}>Logout</MenuItem>        </MenuList>      </ClickAwayListener>    </Paper>  </Grow> )}</Popper>但是,然后我收到一個錯誤:React Hook "React.useRef" 不能在回調(diào)中調(diào)用。React Hooks 必須在 React 函數(shù)組件或自定義 React Hook 函數(shù)中調(diào)用 react-hooks/rules-of-hooks我怎樣才能動態(tài)地做到這一點,以便我可以在 map 函數(shù)中使用 refs。我已經(jīng)嘗試過答案中的建議,但我無法讓它發(fā)揮作用。這是示例的代碼和框。
查看完整描述

2 回答

?
慕運維8079593

TA貢獻1876條經(jīng)驗 獲得超5個贊

這是另一種選擇:


const textInputRefs = useRef<(HTMLDivElement | null)[]>([])


...


const onClickFocus = (event: React.BaseSyntheticEvent, index: number) => {

    textInputRefs.current[index]?.focus()

};


...

{items.map((item, index) => (

    <textInput

        inputRef={(ref) => textInputs.current[index] = ref}

    />

    <Button

        onClick={event => onClickFocus(event, index)}

    />

}


查看完整回答
反對 回復 2021-10-14
  • 2 回答
  • 0 關(guān)注
  • 267 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號