1

could you please tell me how to set value in dropdown in react js ? I am getting dropdown data after few seconds 3000 and then I need to set value on dropdown

const App = ({ children }) => {
  const val = "ax";
  const [state, setState] = useState([]);
  setTimeout(() => {
    setState(countryOptions);
  }, 2000);
  return (
    <Container style={{ margin: 20 }}>
      <Example countryOptions={state} />
    </Container>
  );
};

https://codesandbox.io/s/semantic-ui-example-utev4 expected output Aland Islands should be selected. { key: "ax", value: "ax", text: "Aland Islands" },

as after three second I want to select this element

const val = "ax";
1
  • Updated my answer, due to Stavros answer. It's better to keep what value is selected by default in the App component. Commented Aug 23, 2019 at 8:32

4 Answers 4

2

As stavros answer suggested; it may be better to keep state in App component and pass the setVal to the dropdown:

App:

const App = ({ children }) => {
  const [state, setState] = useState([]);
  //added val and setVal in App state
  const [val,setVal]=useState('ax');
  setTimeout(() => {
    setState(countryOptions);
  }, 2000);
  return (
    <Container style={{ margin: 20 }}>
      //pass val and setVal so dropdown can set val on change
      <Example countryOptions={state} val={val} onChange={setVal}/>
    </Container>
  );
};

Dropdown:

const DropdownExampleClearableMultiple = ({ countryOptions,val,onChange }) => (
  <Dropdown
    clearable
    fluid
    search
    closeOnChange
    selection
    options={countryOptions}
    //set value to passed in val
    value={val}
    //use setVal that was passed in as onChange
    onChange={(_,i)=>onChange(i.value)}
    placeholder="Select Country"
  />
);
Sign up to request clarification or add additional context in comments.

Comments

1

You should update your question because only after visiting the codesandbox was I able to get enough info for an answer..

In your index.js you should update setState(countryOptions) to :

setState({countryOptions:countryOptions},()=>setState({val:"ax"})

Then line 39 to :

<Example countryOptions={state.countryOptions:countryOptions} val={state.val} />

Then in your example.js update const DropdownExampleClearableMultiple to:

const DropdownExampleClearableMultiple = ({ countryOptions, val }) => (
<Dropdown
  clearable
  fluid
  search
  closeOnChange
  selection
  options={countryOptions}
  placeholder="Select Country"
  value={val}
/>
);

3 Comments

Good answer but OP uses hooks. So OP should create a hook for value const [val, setVal] = useState(); and pass val to the component val={val} Also the user cannot change the input unless you pass onChange with value of setVal: onChange={setVal}
You are right. OP should update the dropdown and make it controlled but my understanding is that he/she doesn't have a good understanding of what he/she is doing here so I tried not to add more confusion.
I updated my answer, it would be better to keep state in App component and pass onChange from App
0

Use the "value" prop.

// index.js
const App = ({ children }) => {
  const val = "ax";
  const [state, setState] = useState([]);
  setTimeout(() => {
    setState(countryOptions);
  }, 2000);
  return (
    <Container style={{ margin: 20 }}>
      <Example countryOptions={state} value={val} />
    </Container>
  );
};

// example.js
const DropdownExampleClearableMultiple = ({ countryOptions, value }) => (
  <Dropdown
    clearable
    fluid
    search
    closeOnChange
    selection
    value={value}
    options={countryOptions}
    placeholder="Select Country"
  />
);

1 Comment

Now the user cannot change the value of the dropdown as you never update val on change.
0

You can pass value to value property of Dropdown. Use state property for selected value. Make sure that you change/update state on onchange event.

Comments

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.