Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React component throws React.forwardRef error #884

Open
excelwebzone opened this issue Dec 30, 2023 · 1 comment
Open

React component throws React.forwardRef error #884

excelwebzone opened this issue Dec 30, 2023 · 1 comment

Comments

@excelwebzone
Copy link

When using @emoji-mart/reac I'm getting an error:

chunk-YTQVGCO2.js?v=4616d38d:521 Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

Look like you need to upgrade the component. Here is a new version:

// @ts-nocheck
import React, { useEffect, useImperativeHandle, useRef } from 'react'
import { Picker } from 'emoji-mart'

const EmojiPicker = React.forwardRef((props, ref) => {
  const pickerRef = useRef(null)

  if (pickerRef.current) {
    pickerRef.current.update(props)
  }

  useEffect(() => {
    pickerRef.current = new Picker({ ...props })

    return () => {
      pickerRef.current = null
    }
  }, [])

  // Expose methods or properties via the ref
  useImperativeHandle(ref, () => ({
    update: (newProps) => pickerRef.current?.update(newProps),
    // Add more methods or properties as needed
  }), [props])

  return <div ref={ref} />
})

export default EmojiPicker

Usage:

const emojiPickerRef = useRef(null);

// ...

<EmojiPicker ref={emojiPickerRef} />
@Siddharth-Dagar-25
Copy link


import React, { useEffect, useImperativeHandle, useRef } from 'react';
import { Picker } from 'emoji-mart';

const EmojiPicker = React.forwardRef((props, ref) => {
  const pickerRef = useRef(null);

  useEffect(() => {
    // Initialize the Picker instance
    pickerRef.current = new Picker({ ...props });

    // Cleanup
    return () => {
      pickerRef.current = null;
    };
  }, []); // Empty dependency array to run only on mount and unmount

useImperativeHandle(ref, () => ({
    update: (newProps) => {
      // Check if the pickerRef.current is available and then update
      if (pickerRef.current) {
        pickerRef.current.update(newProps);
      }
    },
    // Add more methods or properties as needed
  }));

  return <div ref={pickerRef} />; // Use pickerRef for the div
});

export default EmojiPicker;

  1. The ref you passed to EmojiPicker is being used to expose methods to the parent component. The pickerRef is used for the div to maintain the reference to the actual DOM element.

  2. I've left the dependency array empty to ensure that the useEffect hook runs only once when the component mounts and when it unmounts. If you want the effect to run when certain props change, you should include those props in the dependency array.

  3. In the update method exposed through useImperativeHandle, there's a check to ensure pickerRef.current is not null before calling update on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants