gpt4 book ai didi

javascript - 来自 Dropdown.Button (antd) 的引用

转载 作者:行者123 更新时间:2023-11-30 13:49:47 25 4
gpt4 key购买 nike

如何获取 Dropdown.Button 的 ref(它是 antd),当我为其设置 ref 时,它会返回一个包装器?

 parent = null;
parentRef = node => (this.parent = node);

render(){
return (
<Dropdown.Button
ref={this.parentRef}
overlay={menu}
type="primary"
>
content
</Dropdown.Button>
)
}

最佳答案

您需要使用引用 API,useRefReact.createRef

引用Refs and the DOM in React :

class App extends React.Component {
buttonRef = React.createRef();

componentDidMount() {
console.log(this.buttonRef);
}

render() {
return (
<Dropdown.Button ref={this.buttonRef} overlay={menu} type="primary">
content
</Dropdown.Button>
);
}
}

带钩子(Hook):

const App = () => {
const buttonRef = useRef();

useEffect(() => {
console.log(buttonRef);
}, []);

return (
<FlexBox>
<Dropdown.Button ref={buttonRef} menu={menu}>
drop down
</Dropdown.Button>
</FlexBox>
);
};

Edit Q-58498231-createRef

关于javascript - 来自 Dropdown.Button (antd) 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58498231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com