S
S
Stergy2018-09-10 15:02:22
JavaScript
Stergy, 2018-09-10 15:02:22

How to change select value by condition?

There is a select form in React, it has several {option1, option2, option3........}
Option elements loaded from a file with such an array:

export default {

  "option1": {
    name: "option1",
    fields: some text
       },

 "option2": {
    name: "option2",
    fields: [{some obj}]
  },

   "option3": {
       name: "option3",
       fields: some text,
       some func(){},
  },

  "option4": {
    name: "option4",
    fields: [],
  }
}

How can I make it so that option4 is loaded only if the condition is met, namely this
if (window.location.search === "?advanced_mode")
    {show option4};

Those. if the suffix ?advanced_mode is present in the address, load option4

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan Gerasimov, 2018-09-10
@Stergy

Something like this:
https://codesandbox.io/s/mq7m3wxo3x
https://mq7m3wxo3x.codesandbox.io/?advanced_mode

B
Bessamuto, 2018-09-10
@Bessamuto

I can tell you to go from the opposite, if you constantly get an object with "option4" then delete it.

if (window.location.search !== "?advanced_mode") {
  let someObject = exportedObject
  delete someObject.option4
  console.log(someObject)
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question