Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-select: How do I resolve “Warning: Prop `id` did not match”

I have a web app with ReactJs and NextJs. In a functional component, I have used react-select then, I'm receiving the following console warning:

Warning: Prop id did not match. Server: "react-select-7-input" Client: "react-select-2-input"

My codes are following:

import { Row, Col, Card, Form, Button } from 'react-bootstrap';
import Select from 'react-select';

const priorityOptions = [
  { value: 'p1', label: 'Top level - P1' },
  { value: 'p2', label: 'Mid level - P2' },
  { value: 'p3', label: 'Low level - P3' }
];
const PostView = () => {
  return (
    <div className="DashboardSla-ContentBody__Form">
      <Row>
        <Col md="10">
          <Card className="shadow-sm">
            <Card.Body>
              <Form>
                <h5 className="text-secondary mb-3">Booking details</h5>
                <Form.Group controlId="postType">
                  <Form.Label>Booking priority</Form.Label>
                  <Select 
                    id="postType"
                    placeholder="Make a selection"
                    options={priorityOptions}
                  />
                </Form.Group>
                <Button
                  type="submit"
                  variant="primary"
                >Add Booking</Button>
              </Form>
            </Card.Body>
          </Card>
        </Col>
      </Row>
    </div>
  )
}

export default PostView;
like image 830
Sonjoy Datta Avatar asked Apr 18 '20 13:04

Sonjoy Datta


3 Answers

try to add prop instanceId set as unique string and should work

like image 156
xargr Avatar answered Oct 19 '22 19:10

xargr


Select component needs an instanceId, id mandatory So Just add

id="long-value-select" instanceId="long-value-select"

to your Select component it will remove the Warning :)

like image 11
Smit Desai Avatar answered Oct 19 '22 20:10

Smit Desai


React 18 introduces a useId hook for generating an ID that is hydration-safe (stable across client and server renders) but still globally unique. You can use it to solve this problem. I use the following component in place of Select:

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

export default function StableSelect({ ...props }) {
  return <Select {...props} instanceId={useId()} />;
}

You can use this component exactly like Select, but it won’t raise any hydration errors since its ID is stable.

like image 7
Luke Taylor Avatar answered Oct 19 '22 18:10

Luke Taylor