5

I am building a reusable component using react-select for selecting US State.. I am trying to pass in a State value (like "OH") to set the default value but can't seem to wrap my head around this..

my data (small sample):

 const statesJson = [
  {
    "label": "Alabama",
    "value": "AL"
  },
  {
  "label": "Alaska",
  "value": "AK"
  },
  {
  "label": "American Samoa",
  "value": "AS"
  },
  {
  "label": "Arizona",
  "value": "AZ"
  },
  {
  "label": "Ohio",
  "value": "OH"
  }
]

My component:

import React, { Fragment, useState} from "react";
import statesJson from "./states";
import Select, { components } from "react-select";
import styled from "styled-components";
import PropTypes from "prop-types";

const StyledSelect = styled(Select)`
  font-size: 16px;
  font-weight: normal;
  color: #041e41;
  width: 250px;
`;

const propTypes = {
    id: PropTypes.string,
    onChange: PropTypes.func,
    className: PropTypes.string
};

const styles = {
    dropdownIndicator: (base: any) => ({
        ...base,
        color: "#65A100"
    }),
    menuList: (base: any) => ({
        ...base,
        height: "auto",
        border: "1px solid #0173c6",

        "::-webkit-scrollbar": {
            width: "9px"
        },
        "::-webkit-scrollbar-track": {
            background: "white"
        },
        "::-webkit-scrollbar-thumb": {
            background: "#0173C6"
        },
        "::-webkit-scrollbar-thumb:hover": {
            background: "#555"
        }
    })
}

const USStates: any[] = statesJson;

export function SelectState(props: any) {
    const [selected, setSelected] = useState();

    return (
        <StyledSelect
            {...props}
            styles={styles}
            value={selected}
            placeholder="Select a State"
            onChange={(item: any) => {
                setSelected(item);
                props.onChange(item.value);
            }}
            options={props.options.map((item: any) => ({ label: item.value + ' - ' + item.label, value: item.value }))}
        />
    );
};

export default function StateSelectDropDown(props: any) {
    return (
        <Fragment>
            <SelectState
                isSearchable
                defaultValue={props.state}
                options={USStates}
                onChange={(item: any) => {
                    alert(item);
                }}
            />
        </Fragment>
    );
}

and code snippet from page:

<div>
  <StateDropDown  state="OH" />
</div>

Any suggestions how to get this to work?

Codesandbox link

2

1 Answer 1

4

You need to provide the full value object for the defaultValue to work. This should work:

export default function StateSelectDropDown(props: any) {
  return (
    <Fragment>
      <SelectState
        isSearchable
        defaultValue={USStates.find(({ value }) => value === props.state)}
        options={USStates}
        onChange={(item: any) => {
          console.log(item);
        }}
      />
    </Fragment>
  );
}
Sign up to request clarification or add additional context in comments.

2 Comments

That worked, don't know how I missed it.. ugh
worked with value={...} instead of defaultValue={...} for me. Thanks!

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.