admin管理员组文章数量:1300019
I am a beginner, trying to understand react-popper
. On their github, this is the example code:
import { Manager, Reference, Popper } from 'react-popper';
const Example = () => (
<Manager>
<Reference>
{({ ref }) => (
<button type="button" ref={ref}>
Reference element
</button>
)}
</Reference>
<Popper placement="right">
{({ ref, style, placement, arrowProps }) => (
<div ref={ref} style={style} data-placement={placement}>
Popper element
<div ref={arrowProps.ref} style={arrowProps.style} />
</div>
)}
</Popper>
</Manager>
);
Is this as it stands there supposed to be working? I am not sure I understand how I could use this code, how it would look like if I were to import/use this Example
Component into my own parent ponent; Would I need to pass refs
? How so? It would be great if somebody could give me a working example of this code, I'd just like to see how to make use of this.
I am a beginner, trying to understand react-popper
. On their github, this is the example code:
import { Manager, Reference, Popper } from 'react-popper';
const Example = () => (
<Manager>
<Reference>
{({ ref }) => (
<button type="button" ref={ref}>
Reference element
</button>
)}
</Reference>
<Popper placement="right">
{({ ref, style, placement, arrowProps }) => (
<div ref={ref} style={style} data-placement={placement}>
Popper element
<div ref={arrowProps.ref} style={arrowProps.style} />
</div>
)}
</Popper>
</Manager>
);
Is this as it stands there supposed to be working? I am not sure I understand how I could use this code, how it would look like if I were to import/use this Example
Component into my own parent ponent; Would I need to pass refs
? How so? It would be great if somebody could give me a working example of this code, I'd just like to see how to make use of this.
2 Answers
Reset to default 4I am guessing that you aim for a tooltip-like functionality. Is it what you are trying to achieve ?
Bear in mind that react-popper has a low level API. It is a positional engine rather than a tooltip library. By using it you would have to implement the logic of displaying the Popper yourself. For instance you could have a variable shouldShowElement
and trigger changes to this
...
<Manager>
<Reference>
{({ ref }) => (
<button
type="button"
ref={ref}
onMouseEnter={() => {
/* [[SOME CODE TO SET shouldShowElement TO TRUE]] */
}}
onMouseLeave={() => {
/* [[SOME CODE TO SET shouldShowElement TO FALSE]] */
}}
>
Reference element
</button>
)}
</Reference>
{shouldShowElement ? (
<Popper placement="right">
{({ ref, style, placement, arrowProps }) => (
<div ref={ref} style={style} data-placement={placement}>
Popper element
<div ref={arrowProps.ref} style={arrowProps.style} />
</div>
)}
</Popper>
) : null}
</Manager>
...
If you want a more high-level API you can use react-tippy or react-popper-tooltip
Don't bother too much with the render props. In most of the cases, you won't need to modify them.
Just keep in mind that
ref
render prop fromReference
should be attached to the reference element (your item where is attached your popper).ref
render prop fromPopper
should be attached on the popper (your tooltip or whatever).
style
and placement
render prop is for the geographical position on the page.
In resume, this example will render a button with an arrowed popperized <div>
which contains "Popper element"
.
本文标签: javascriptHow to use reactpopper with render propsStack Overflow
版权声明:本文标题:javascript - How to use react-popper with render props - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741655336a2390726.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论