简体   繁体   中英

React setstate with datamaps?

I am using Datamaps for this example. I have a Datamap that, when clicked in .datamaps-subunit.MI , changes the color of the portion. I would also like to store a number into my React state. However, the code below results in a this.setState is not a function error.

lemap:function(){
    var map = new Datamap({
        element: document.getElementById('maincontains'),
        scope: 'usa',
        fills: {defaultFill: 'rgb(217, 217, 217)'},
        done: function(datamap) {
            datamap.svg.select('.datamaps-subunit.MI').on('click', function(geography) {
                var m = {};
                var bis = this;
                m[geography.id] = 'pink';
                datamap.updateChoropleth(m);
                this.setState({trumpnum: 12});
            });
        }
    }); 
},

Fixed it by adding var sting = this right beneath the function.

lemap:function(){
var sting = this;
    var map = new Datamap({
        element: document.getElementById('maincontains'),
        scope: 'usa',
        fills: {defaultFill: 'rgb(217, 217, 217)'},
        done: function(datamap) {
            datamap.svg.select('.datamaps-subunit.MI').on('click', function(geography) {
                var m = {};
                var bis = this;
                m[geography.id] = 'pink';
                datamap.updateChoropleth(m);
                sting.setState({trumpnum: 12});
            });
        }
    }); 
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM