John Smith
John Smith

Reputation: 6259

Simple React Component does not update

I have the following component: Somehow DropdownMultiselect does not update, even though the variables changed. What do I wrong here?

function Sidebar({chartOptions}) {
  let variables = []
  if(chartOptions){
    variables = chartOptions
  }

  return (
    <DropdownMultiselect options={variables} name="variables"/>
  )
}

Upvotes: 0

Views: 43

Answers (1)

JeromeBu
JeromeBu

Reputation: 1159

You cannot mutate variable like that in React. You can use the prop directly, using ?? to handle the case where chartOptions is null or undefined :

function Sidebar({chartOptions}) {
  return (
    <DropdownMultiselect options={chartOptions ?? []} name="variables"/>
  )
}

Upvotes: 1

Related Questions