Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. const nodeRef = useCallback(node => {
  2. if (node !== null) {
  3. M.Dropdown.init(node);
  4. }
  5. }, []);
  6.  
  7. <div
  8. ...
  9. ref={nodeRef}
  10. data-target="dropdown1"
  11. >
  12.  
  13. {visible && <Component />}
  14.  
  15. DOMException: "Node was not found"
  16.  
  17. const useFabInit = ref => {
  18. // Load the Material CSS dynamically
  19. // SSR issue as "window" is not available on server side.
  20. useEffect(() => {
  21. // WebpackError: ReferenceError: window is not defined
  22. if (!window) return
  23. if (!ref || !ref.current) return
  24.  
  25. import("materialize-css").then(({ default: M }) =>
  26. M.FloatingActionButton.init(ref.current, { hoverEnabled: false })
  27. )
  28. }, [])
  29. }
  30.  
  31. const Fabby = () => {
  32. const [isOpen, setIsOpen] = useState(false)
  33. const fabRef = useRef(undefined)
  34. const { names: blockNames, meta } = useBlockContext()
  35.  
  36. 2️⃣ 👇 Initialize the floating action button
  37. useFabInit(fabRef)
  38.  
  39. // 3️⃣ Use the ref here safely Handle open/close toggling
  40. useEffect(() => {
  41. const toggleOpen = () => setIsOpen(open => !open)
  42. fabRef.current.addEventListener("click", toggleOpen)
  43. return () => fabRef.current.removeEventListener("click", toggleOpen)
  44. }, [])
  45.  
  46. const buttons = blockNames.map(({ name, id }) => {...})
  47.  
  48. return (
  49. 1️⃣ 👇 ref uses a callback
  50. <div ref={fabRef} className="fixed-action-btn">
  51. <a className={cn("btn-floating btn-large", isOpen ? "blue" : "red")}>
  52. <i className="large material-icons">mode_edit</i>
  53. </a>
  54. <ul>{buttons}</ul>
  55. </div>
  56. )
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement