1

I'm currently rendering two types of checkboxes (checked or unchecked) depending on some logic. As you can see this code is not very DRY, since the only difference between the two lines is the defaultValue attribute.

if (user.surveyData.device.indexOf(device) > -1) {
    devicesList.push(<li key={i}><input type="checkbox" className="device" value="DK2" defaultChecked/>{ device }</li>)
} else {
    devicesList.push(<li key={i}><input type="checkbox" className="device" value="DK2"/>{ device }</li>)
}

Is there any way I can do this a little more DRY-ly?

3
  • 1
    I answered to a question very similar to this one here: stackoverflow.com/questions/31163693/… Commented Apr 11, 2016 at 16:50
  • 1
    defaultChecked={user.surveyData.device.indexOf(device) > -1} Commented Apr 11, 2016 at 16:52
  • worked like a charm, thanks guys. marked this one as a duplicate Commented Apr 11, 2016 at 16:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.