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.

Changes to defaultSelectedItem prop does not impact re-rendered input

See original GitHub issue

Thanks for an awesome library! My issue may be related to incorrect usage of the downshift API, but after reviewing the docs, I’m still not sure of the proper way to handle this problem. In short, I’m finding changes in the defaultInput and defaultSelectedItem props in downshift do not result in changes in getInputProps and selectedItem in the arguments in the render prop.

Dependencies:

  • downshift version: 1.28.1
  • node version: 8.9.4
  • npm (or yarn) version: 1.5.1 (yarn)

What I did:

Please check out this forked example in codesandbox. Here’s the relevant snippet:

<ExampleDownshift
  onStateChange={this.handleStateChange}
  // modifying to a controlled input (selectedItem) results in change
  defaultSelectedItem={{ name: this.state.defaultName }}
  onChange={this.handleChange}
  items={this.state.items}
  itemToString={this.itemToString}
/>

I’m then toggling a button that changes the state of the component that wraps ExampleDownshift.

What happened:

When ExampleDownshift re-renders and this.state.defaultName has a new value, the component rendered by Downshift’s render prop does not render the new defaultSelectedItem. In the linked example, my expectation would be that Not Luke Skywalker becomes the new selectedItem instead of Luke Skywalker when pressing the Change default button.

Suggested solution:

I do not have a solution to offer, but my workaround right now is the following (sandbox example here):

const WrappedDownshift = () => (
  <ExampleDownshift
    onStateChange={this.handleStateChange}
    defaultSelectedItem={{ name: this.state.defaultName }}
    onChange={this.handleChange}
    items={this.state.items}
    itemToString={this.itemToString}
  />
);

When WrappedDownshift is rendered, this change now always results in a new Downshift being rendered. I think without this change, Downshift will just call componentWillReceiveProps on each render since it is already mounted, whereas this change will call the component constructor and reset this.state.selectedItem. I am not sure that changing state on componentWillReceiveProps is desirable behavior, but it would resolve this issue I think.

Thanks again for the awesome library; definitely want to be clear that this isn’t necessarily a change request; it’s asking how to use Downshift when you want to change the default selected input.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

1reaction
BenBrostoffcommented, Mar 1, 2018

Thanks @donysukardi and @austintackaberry - these explanations make sense to me. I’ll use a controlled component 👍

1reaction
austintackaberrycommented, Mar 7, 2018

What about something like this:

https://codesandbox.io/s/431w33k8ox

Note that the clearSelection button doesn’t work in my example. The error isn’t very helpful, so I’m not sure exactly what is breaking it, but I think it has something to do with selectedItem being set to null when it’s shape should be {name: someValue}. Nonetheless, the codesandbox should be close to what you’re looking for.

EDIT: I fixed the clearSelection issue 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

defaultValue change does not re-render input - Stack Overflow
I found what seems to be a pretty good solution to this: Use the key prop to force rendering of an entirely new...
Read more >
Solved: DefaultSelectedItems not not available when used o...
Hi. I have a Combo Box which has a propert DefaultSelectedItems which has a formual in it which works OK. On a button,...
Read more >
https://unpkg.com/office-ui-fabric-react@7.14.0/di...
Whether or not autofill is enabled depends on the input value, * whether or not any text is selected, and only if the...
Read more >
does a component rerender only when its props change, or ...
It means that if you're working in a memorized child component you can be not rerendering when you would expect to be and...
Read more >
https://raw.githubusercontent.com/microsoft/fluent...
fluentui/react_v8.104.1) ### Patches - fix: ComboBox button is not ... Fixed bug where className changes in List props does not cause a rerender...
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