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.

[v2.0.0-beta.2] Custom ValueContainer doesn't work without rendering "children"

See original GitHub issue

I’m trying to use the ValueContainer option to render the selected option differently. Seems I cannot do that and still let the selected option (ValueContainer) be clickable and open the menu without also rendering the children prop. However, in rendering the children prop, I’m basically rendering the selected option twice. Clicking the arrow still works though.

Basically, this works fine (example from docs):

const ValueContainer = ({children, ...props}) => (
  <components.ValueContainer {...props}>{children}</components.ValueContainer>
);

This does not work:

const ValueContainer = ({children, ...props}) => {
  if (!props.hasValue) {
    return <components.ValueContainer {...props}>{children}</components.ValueContainer>;
  }

  const value = props.getValue()[0];
  return (
    <components.ValueContainer {...props}>
      <NodeItem node={value} />
    </components.ValueContainer>
  );
};

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:35
  • Comments:29

github_iconTop GitHub Comments

23reactions
Rall3ncommented, Mar 22, 2019

You could filter out the input from children so you can still keep standard functionality:

const ValueContainer = ({children, ...props}) =>{
    var selectInput = React.Children.toArray(children).find((input) => input.type.name === "Input" || input.type.name === "DummyInput");
   
    return <components.ValueContainer { ...props }>
        { /* Whatever you want to render */ }
        {selectInput}
   </components.ValueContainer>
}
9reactions
brahmdevcommented, Jan 28, 2020

We were also facing this problem for a while. And below is the working code for us.

  1. We added inputId prop to <Select> component like below:
<Select
          className='select'
          classNamePrefix='filter'
          inputId='clickableInput'
          isMulti
          components={{ ValueContainer }}
         ....
        />
  1. And below is our return from ValueContainer:
return (
      <components.ValueContainer {...props}>
       // code for some custom element if you need goes here
        {React.Children.map(children, (child) => {
          return child.props.id === 'clickableInput' ? child : null;
        })}
      </components.ValueContainer>
    );
Read more comments on GitHub >

github_iconTop Results From Across the Web

[v2.0.0-beta.2] Custom ValueContainer doesn't work without ...
[v2.0.0-beta.2] Custom ValueContainer doesn't work without rendering "children"
Read more >
React-Select losing focus for custom component ...
ValueContainer has two objects when there's no value selected: ... out that you should put the custom value container outside the render
Read more >
react-select-fixed-v2/HISTORY.md
This is part of the work to normalize the behaviour circa custom components. ... 319, * Fixed inline-block rendering for arrowRenderer without autosize, ......
Read more >
React-select custom ValueContainer
CodeSandbox is an online editor tailored for web applications.
Read more >
https://raw.githubusercontent.com/Kashkovsky/react...
This is part of the work to normalize the behaviour circa custom components. ... Fixed inline-block rendering for arrowRenderer without autosize, ...
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