Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. import { useEffect, useState } from 'react';
  2. import $ from "jquery"
  3.  
  4. /**
  5. @example
  6. const ref = useRef<HTMLDivElement>(null)
  7.  
  8. usejQuery(ref, $ => {
  9. $.fadeOut(3000).fadeIn(3000)
  10. })
  11.  
  12. const text = usejQuery(ref, $ => {
  13. return $.text()
  14. })
  15.  
  16. useEffect(()=> {
  17. console.log(text)
  18. },[text])
  19. */
  20. export const usejQuery = <T extends HTMLElement, S extends any>(ref: React.RefObject<T>, callback: ($el: JQuery<T>) => S): S => {
  21. const [state, setState] = useState<S>()
  22. useEffect(()=> {
  23. if(ref && ref.current){
  24. setState(callback($(ref.current)))
  25. }
  26. },[ref])
  27. return state
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement