question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React select passing props for components to useMemo

See original GitHub issue

I am trying to use useMemo for the Option, MultiValue custom component in the react select.

When I tried it getting issue, am I missing something in the below code snippet?

Getting props undefined, what can be the issue, didn’t find much reference on this

Sample Snippet

import React, { useMemo } from "react";
import Select, { components } from "react-select";

const options = [
  { value: "chocolate", label: "Chocolate" },
  { value: "strawberry", label: "Strawberry" },
  { value: "vanilla", label: "Vanilla" }
];

const Option = useMemo(() => {
  return (
    <div>
      <components.Option {...props}>
        <checkbox checked={props.isSelected} onChange={() => null} />
        <label>{props.label}</label>
      </components.Option>
    </div>
  );
}, [props]);

const MultiValue = useMemo(() => {
  return (
    <components.MultiValue {...props}>
      <span>{props.data.label}</span>
    </components.MultiValue>
  );
}, [props]);

const CustomSelect = () => (
  <Select options={options} isMulti components={{Option, MultiValue}} />
);

export default CustomSelect;

Reproducible code link

If i remo useMemo hook it works, but not understanding as it should work !! confused

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
camcnzcommented, Sep 14, 2020

I can see what @dileepthomas is getting at. It would be great to have an example of how (if possible) to use react-select with memo and Hooks. For me currently React.memo(Select) doesn’t work because you have to set double curly brackets ‘{{’ in components={{SingleValue}}. This is creating a new object each render therefore triggering memo to rerender as it’s a new prop.

0reactions
bladeycommented, Jul 16, 2020

Thanks for your help here @Rall3n.

@dileepthomas I’m going to close this now as @Rall3n points out this isn’t react-select related.

Please let me know if you think otherwise.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React select passing props for components to useMemo
1 Answer 1 ; const Option ; useMemo · () => ; return · <div> <components.Option {...props}> <checkbox checked={props.isSelected} onChange={() => null} ...
Read more >
useMemo - React Docs
You pass it as a prop to a component wrapped in memo . You want to skip re-rendering if the value hasn't changed....
Read more >
How to Memoize with React.useMemo() - Dmitri Pavlutin
During initial rendering, useMemo(compute, dependencies) invokes compute , memoizes the calculation result, and returns it to the component. If ...
Read more >
How to properly receive props data in react-select's custom ...
You can access the props passed to the Select component using props.selectProps inside your Control component. import React from "react"; import ...
Read more >
[Solved]-React Select MenuList Props-Reactjs - appsloveworld
React limits the number of renders to prevent an infinite loop." Can't pass a property (as props) of fetched data object from child...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found