Jason Chen Jason Chen - 7 months ago 27
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.


JavaScript bracket notation:

if (sting.state[state] === 'und')