81

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 code is the 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;
starball
  • 20,030
  • 7
  • 43
  • 238
Sonjoy Datta
  • 1,188
  • 2
  • 13
  • 21
  • 3
    Have you tried to set the `instanceId`? See here: https://react-select.com/props and https://github.com/JedWatson/react-select/issues/2629 – David Apr 18 '20 at 15:52
  • 1
    I have replaced `id="postType"` as `instanceId="postType"`. Still showing error in console. – Sonjoy Datta Apr 19 '20 at 05:11
  • 1
    Did you ever figure this one out? I think it has something to do with rehydration of an SSR React app but not sure how to solve that. – serraosays Jul 03 '20 at 22:15

5 Answers5

144

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

xargr
  • 2,788
  • 1
  • 19
  • 28
25

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.

Luke Taylor
  • 8,631
  • 8
  • 54
  • 92
15

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 :)

Smit Desai
  • 161
  • 1
  • 2
1
<Select
  className={className}
  name={field.name}
  id="long-value-select"
  instanceId="long-value-select"
  value={getValue()}
  onChange={onChange}
  placeholder={placeholder}
  options={options}
  isMulti={isMulti}
/>
  • 2
    Please read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others. – Saeed Zhiany Jul 17 '22 at 03:50
0

Instanceid should be any string in react select props it will definitely work...

instanceId = "select-box"
yuvraj sune
  • 191
  • 1
  • 2