React select passing props for components to useMemo
See original GitHub issueI 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:
- Created 3 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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.