0

I am building a custom dropdown with hooks and I ran into this issue in which the ref.current for the li elements rendered from an array is undefined. However, my other ref isn't. I'm sure I am doing something wrong, I just can't seem to pin point if it's losing or failing to bind the .current property.

I'm using styled-components, thus the naming convention.

How come input succesfully references the correct jsx element, but link is undefined?

I am exploring strategy to use keyboard navigation entirely with hooks, so if there are any suggestion, glad to hear them :D

const CustomDropdown({ handleCategoryClick, width }) => {
    const input = useRef();
    const link = useRef();

    useEffect(() => {
        input.current.focus();
    }, []);

    const toggleList = () => {
        if (isOpen) {
            setIsOpen(false);
            return;
        }
        setIsOpen(true);
        setOptions(dropdownOptions);
    };
return (
    return (
        <DDWrapper>
            <DropdownContainer>
                <InputContainer onClick={toggleList}>
                    <DropdownInput
                        ref={input}
                        name="customDropdown"
                        width={width}
                        isOpen={isOpen}
                        defaultValue={inputValue}
                        placeholder="Select a filter"
                    />
                </InputContainer>
            </DropdownContainer>
            {isOpen && (
                <DDList width={251} isOpen={isOpen} onClick={handleCategoryClick} role="navigation">
                    {options.map(({ label }, i) => (
                        <DDItem
                            id="item"
                            onKeyDown={handleKeyDown}
                            onClick={hideList}
                            data-value={label}
                            key={i}
                            width={width}
                            ref={link}
                        >
                            {label}
                        </DDItem>
                    ))}
                </DDList>
            )}
        </DDWrapper>
    );
aromanarguello
  • 626
  • 4
  • 11
  • 23

0 Answers0