Lef*_*eff 3 javascript reactjs react-ref
我有一个table列表,其中每一行都有一个menu按钮,为此我需要一个ref. 我在我的项目中使用 react mui,它是menu。我试过像这样创建参考:
const {rows} = props;
const refs = Array.from({length: rows.length}, a => React.useRef<HTMLButtonElement>(null));
Run Code Online (Sandbox Code Playgroud)
然后尝试map在每个函数上使用像这样的内部函数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>
Run Code Online (Sandbox Code Playgroud)
但是,然后我收到一个错误:
React Hook "React.useRef" 不能在回调中调用。React Hooks 必须在 React 函数组件或自定义 React Hook 函数中调用 react-hooks/rules-of-hooks
我怎样才能动态地做到这一点,以便我可以在 map 函数中使用 refs。我已经尝试过答案中的建议,但我无法让它发挥作用。这是示例的代码和框。
Jöc*_*ker 14
这里还有另一个选择:
const textInputRefs = useRef<(HTMLDivElement | null)[]>([])
...
const onClickFocus = (event: React.BaseSyntheticEvent, index: number) => {
textInputRefs.current[index]?.focus()
};
...
{items.map((item, index) => (
<textInput
inputRef={(ref) => textInputRefs.current[index] = ref}
/>
<Button
onClick={event => onClickFocus(event, index)}
/>
}
Run Code Online (Sandbox Code Playgroud)
useRef与 React.createRef 不完全相同。最好叫它useInstanceField:)
所以,你的代码可能有点不同。
第一步:我们useRef用来保存 refs 数组:
const {rows} = props;
const refs = useRef(Array.from({length: rows.length}, a => React.createRef()));
Run Code Online (Sandbox Code Playgroud)
然后,在您的 map 函数中,我们将每个 ref 保存到 refs 数组中的索引:
<Button
ref={refs.current[index]}
aria-controls="menu-list-grow"
aria-haspopup="true"
onClick={() => handleToggle(row.id)}
>Velg
</Button>
<Popper open={!!checkIfOpen(row.id)} anchorEl={refs.current[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.current[index].current)}>
<MenuList>
<MenuItem
onClick={(e) => handleClose(e, refs.current[index].current)}>Profile</MenuItem>
<MenuItem onClick={(e) => handleClose(e, refs.current[index].current)}>My account</MenuItem>
<MenuItem onClick={(e) => handleClose(e, refs.current[index].current)}>Logout</MenuItem>
</MenuList>
</ClickAwayListener>
</Paper>
</Grow>
)}
</Popper>
Run Code Online (Sandbox Code Playgroud)
如果你的长度改变了,你应该处理它useEffect以改变 refs 的长度
您还可以使用另一种方式:
1) 创建一个 refs 数组,但没有 React.createRef:
const {rows} = props;
const refs = useRef(new Array(rows.length));
Run Code Online (Sandbox Code Playgroud)
在地图中我们ref={el => refs.current[index] = el}用来存储 ref
<Button
ref={el => refs.current[index] = el}
aria-controls="menu-list-grow"
aria-haspopup="true"
onClick={() => handleToggle(row.id)}
>Velg
</Button>
<Popper open={!!checkIfOpen(row.id)} anchorEl={refs.current[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.current[index])}>
<MenuList>
<MenuItem
onClick={(e) => handleClose(e, refs.current[index])}>Profile</MenuItem>
<MenuItem onClick={(e) => handleClose(e, refs.current[index])}>My account</MenuItem>
<MenuItem onClick={(e) => handleClose(e, refs.current[index])}>Logout</MenuItem>
</MenuList>
</ClickAwayListener>
</Paper>
</Grow>
)}
</Popper>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4828 次 |
| 最近记录: |