JedWatson / react-select

The Select Component for React.js
https://react-select.com/
MIT License
27.57k stars 4.12k forks source link

React Select not working on React 18.2.0 #5791

Closed jcabrerazuniga closed 11 months ago

jcabrerazuniga commented 11 months ago

I created the next component using React 18.2.0:

import React, { Component } from 'react'; import Select from 'react-select';

export class Position extends Component { static displayName = Position.name;

constructor(props) {
  super(props);
  this.state = { 
        selectedOption: 'Chocolate',
        positionId: '5',
        userId: '3',
        organizationId: '3',
        organizationName: 'Test Org Name',
        positionTitle: "some title",
        startYear: "2000",
        endYear: "2021",
        index: 0,
  };      

}

setSelectedOption = () => {}

componentDidMount()
{
  console.log('componentDidMount>orgIdsToNamesArray4Select = 543636');
  console.log(this.props.orgIdsToNamesArray4Select);
}

render() {
  let item = this.props.item;

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

  return (
    <div>
      <p>organization</p>

      <p>Position Title: {item.positionTitle}</p>          
      <p>Start Year:     {item.startYear}</p>
      <p>End Year:       {item.endYear}</p>

      <Select
          defaultValue={this.state.selectedOption}
          onChange={this.setSelectedOption}
          options={options}
      />   

    </div>
  );
 }

}

Yet, I continue getting then next error:

react.development.js:209 Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.

Is there a React version I should limit myself to work with?

Thanks Jose

lamhungypl commented 11 months ago

you should provide a sandbox link. Just one thing I wonder why there's still a new-created Class component in 2023?

jcabrerazuniga commented 11 months ago

I don't understand... I am using dotnet 7 to create a dotnet react application and while compiling I get the mentioned error. I do prefer to use react classes. I also read the Select component code and uses React 16 so there might be a compatibility issue with 18.

jcabrerazuniga commented 11 months ago

It started to work. It seems I wrongly installed the Select component as it needed to be within the ClientApp folder.