Jason Chen Jason Chen - 1 year ago 157
Javascript Question

call React class with a variable string?

This example uses Reactjs and Datamaps.

I am making a tool that is supposed to select states from a map. A portion of that code is below.

datamap.svg.selectAll('.datamaps-subunit.MI').on('click', function(){
var state = 'mi';
if (sting.state.mi === 'und'){
sting.setState({trumpnums: sting.state.trumpnums + sting.state.miv, mi: 'right'});
datamap.svg.selectAll('.datamaps-subunit.MI').on('mouseout', function(){

To save me from having to repeat a lot of this for each state, I was hoping to store my React classname in a variable.

All instances of
refer to Michigan. So, I was hoping that instead of having to type
, I was hoping for something like

var state = 'mi';
if (sting.state.{state} === 'und'){//rest of my code}

I already set a placeholder for
var state
as you can see in the code above. However, it does not work with my React class.

Answer Source

JavaScript bracket notation:

if (sting.state[state] === 'und')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download