0

I'm using the react-select library in a React project to render a dropdown select box. I have defined an options array that contains a single option with the label "a". I have also defined an onChange function that should log the selected option to the console when an option is selected.

Additionally, I'm using the react-bootstrap library's Modal component to create a popup, and the react-draggable library's Draggable component to make the popup draggable.

Here's my code:


import React from "react";
import Select from "react-select";
import Modal from 'react-bootstrap/lib/Modal'
import Draggable from 'react-draggable'

function App() {
  const onChange = (selectedOption) => {
    console.log("Selected Option:", selectedOption);
  };

  const options = [
    {
      label: "a",
    },
  ];

  const style={
    width:"100px",
    height:"100px",
    backgroundColor:"green"
  };  

  return (
    <div>
      <Draggable>
        <Modal show={true}>
          <Modal.Body>
            <div style={style}>Comp</div>
          </Modal.Body>
        </Modal>
      </Draggable>
      <div style={style}>
        Comp2
      </div>
      <Select options={options} onChange={(option) => onChange(option)} />
    </div>
  );
}

export default App;

Despite everything appearing to be set up correctly, when I open the dropdown and select the "a" option, the onChange function is not being called.

Can anyone help me figure out why the onChange function is not being called when an option is selected?

3
  • Select seems to be a component, since written with capital 'S'. What happens inside that component does it use the onChange property you send to it? Commented Apr 30, 2023 at 13:31
  • Your code works Commented Apr 30, 2023 at 13:34
  • Thank you for your help, but I am actually looking for a way to make the modal window appear and also be able to use the Select component. Commented May 1, 2023 at 15:34

2 Answers 2

0

In the options object, each element must contain a value property.

const options = [
    {
        value: "a",
        label: "a",
    },
];
Sign up to request clarification or add additional context in comments.

2 Comments

This is an example I created to post a code snippet here. The problem is not related to the "options" array but to the fact that the "onChange" function is not being called.
Try setting the initial value to something, so the option actually changes the selected value.
0

Everything is working fine, but you need to set your modal show={false} initially.

import React from 'react';
import Select from 'react-select';
import { Modal } from 'react-bootstrap';
import Draggable from 'react-draggable';

export default function App(props) {
  const onChange = selectedOption => {
    console.log('Selected Option:', selectedOption);
  };

  const options = [
    {
      label: 'a',
    },
  ];

  const style = {
    width: '100px',
    height: '100px',
    backgroundColor: 'green',
  };
  return (
    <div>
      <Draggable>
        <Modal show={false}>
          <Modal.Body>
            <div style={style}>Comp</div>
          </Modal.Body>
        </Modal>
      </Draggable>
      <div style={style}>
        Comp2
      </div>

      <Select options={options} onChange={option => onChange(option)} />
    </div>
  );
}

2 Comments

Thank you for your help, but I am actually looking for a way to make the modal window appear and also be able to use the Select component.
When a modal is open Unfortunately, there's no event. you need to check other solutions rather than a modal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.