Skip to content

React-select multi select update a prop when no value is selected

I am using react-select library to create a multi-selection menu. When there is no value selected I want a certain style width to be used on the div and when a user starts selecting values from the select dropdown, I want this width to be set to null so that the select component can use it’s own auto adjusting width capability. I have the below code but I can’t get the width to update synchronously as it updates after the next render call. Not sure how to make it update with the new width immediately. I know setState is async but if you use a callback in the setState function I would imagine it would render with the new state.

constructor(props) {
    this.state = {
        dropBoxWidth: { width: 130 },
        selectLength: 1
handleChange = selectedOption => {
    if (this.state.selectLength > 0) {
            { selectedOption, selectLength: selectedOption.length, dropBoxWidth: null },
            () => console.log(`Option selected:`, this.state.selectedOption, this.state.selectLength, this.state.dropBoxWidth)
    } else {
        this.setState({ dropBoxWidth: { width: 130 }, selectLength: 1 }), () =>
            console.log("New Dropbox Width ", this.state.dropBoxWidth)

render() {
    return (
        <div style={this.state.dropBoxWidth}>
                placeholder="Item Select"

Again to be clear, I want the style width of the div to be set to 130 when there is no value selected. This can be when the page is opened or refreshed(constructor props has the width to 130), and if a user selects values then decides to clear all the selections from the menu.


You can use inital state in styles of component.

    container: (provided, state) => ({
      width: !state.hasValue && "130px",
      borderBottom: "1px dotted pink"

Like here:

React select styles doc: