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.

Provide an easy way to extend react-select to make it work with other data structures

See original GitHub issue

Immutable data structures become more and more popular in React community. But one of the biggest issues of using them is that there is no straightforward way to integrate them with third-party components like react-select. That’s why we end up with converting immutable data to plain js and back. I suggest to provide several facade-methods for array manipulation, that anyone can easily override to provide support for any data type he wants. These methods are _isArray, _getArrayLength, _getArrayItem and so on.

So originally we will have something like this

{
  _isArray(array) {
    return Array.isArray(array)
  },
  _getArrayLength(array) {
    return array.length;
  },
  _getArrayItem(array, index) {
    return array[index]
  }
}

but if we need, for example Immutable.js support, then we can extend react-select in the following way

import Select from 'react-select';
import {List} from 'immutable';

class ImmutableReactSelect extend Select {
  _isArray(array) {
    return Immutable.List.isList(array) || Array.isArray(array)
  },
  _getArrayLength(array) {
    return  Immutable.List.isList(array) ? array.size : array.length;
  },
  _getArrayItem(array, index) {
    return Immutable.List.isList(array) ? array.get(index) : array[index];
  }
}

and now we can use

import React from 'react';
import {List, Record} from 'immutable';
import ImmutableReactSelect from './ImmutableReactSelect';

const Option = Record({ value: '', label: '' });
const options = List([
   new Option({ value: 'one', label: 'One' }),
   new Option({ value: 'two', label: 'Two' }),
   new Option({ value: 'three', label: 'Three' })
]);

export default class App extends React.Component {
  render() {
    return (
      <ImmutableReactSelect
        name="form-field-name"
        value='one'
        options={options}
      />
    );
  }
}

I have a PR for these changes. If you guys are interested, I can submit it.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:8
  • Comments:13

github_iconTop GitHub Comments

1reaction
stipsancommented, Sep 28, 2017

@kylemh unfortunately no, I had to resort to toJS. To make it less painful I followed the Higher Order Pattern described in the redux docs.

1reaction
Webkadabracommented, Nov 6, 2015

I would vote for that, immutable is awesome 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Styles - React Select
The recommended way to provide custom styles to react-select is to use the styles prop. styles takes an object with keys to represent...
Read more >
To extend the react-select interface property in typescript
3 Answers 3 ; 'react' import · from 'lodash' ; import Select · GroupBase, Props ; from 'react-select' · SelectProps<Option ...
Read more >
Integrating with Other Libraries - React
The easiest way to avoid conflicts is to prevent the React component from updating. You can do this by rendering elements that React...
Read more >
react-select: An introduction - LogRocket Blog
Learn how to use react-select component, how to get started, and how to extend some of the predefined components to suit your needs....
Read more >
Testing a Custom Select with React Testing Library
The other option is to trust that react-select is going to make a component that works and that your users can use. Based...
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