react-select icon indicating copy to clipboard operation
react-select copied to clipboard

How to properly receive props data in react-select's custom component?

Open stonix26 opened this issue 4 years ago • 1 comments

Discussed in https://github.com/JedWatson/react-select/discussions/5340

Originally posted by stonix26 September 13, 2022 I created a custom component to customize the control field, for me to place a custom label with animation. And I can't figure out where to place or receive that prop.

import React from "react";
import { components, ControlProps, GroupBase, Props } from "react-select";
import ReactSelectAsync, { AsyncProps } from "react-select/async";

type TCustomAsyncSelect = (
  props: Props &
    Pick<
      AsyncProps<unknown, boolean, GroupBase<unknown>>,
      "defaultOptions" | "cacheOptions" | "loadOptions" | "isLoading"
    >
) => JSX.Element;

const Control:
  | React.ComponentType<ControlProps<unknown, boolean, GroupBase<unknown>>>
  | undefined = (props) => (
  <div style={{ backgroundColor: "pink", padding: "10px 20px" }}>
    <p>dynamic label title here</p>
    <components.Control {...props} />
  </div>
);

const CustomAsyncSelect: TCustomAsyncSelect = (props) => (
  <ReactSelectAsync {...props} components={{ Control }} />
);

export default CustomAsyncSelect;

I tried placing the custom control component inside my CustomAsyncSelect component, so that I can easily receive my label prop from my CustomAsyncSelect, but it seems there's an issue during render, it loses focus on every keystroke.

Here's my codesandbox for you to play around with.

stonix26 avatar Sep 13 '22 02:09 stonix26

@stonix26 Please do not open an issue for a discussion minutes after its creation. If the problem identifies as an issue, it will be elevated accordingly. But at first glance this is merely a question regarding implementation.

I will close this for now. Conversation should be continued in the linked discussion.

Rall3n avatar Sep 13 '22 14:09 Rall3n