kirjs / react-highcharts

React wrapper for Highcharts library
http://kirjs.github.io/react-highcharts/
MIT License
1.26k stars 233 forks source link

Cannot read property 'dispatch' of undefined #348

Closed ptchiangchloe closed 5 years ago

ptchiangchloe commented 7 years ago

I try to make my series data has click event with redux, however, I got problem when I try to dispatch my function in the callback, I'm using React-Highcharts library, I've also tried to access the chart after the component mounted, but I'm not sure how to do that since there are no example on that.

import React, { Component } from 'react'
import ReactHighcharts from 'react-highcharts'
import {connect} from 'react-redux'
import autobind from 'react-autobind'
import '../style.scss'
import axios from 'axios';
import { handleKeywordTweets } from '../actions'
import { store } from '../../../app.jsx'
require('highcharts/modules/wordcloud.js')(ReactHighcharts.Highcharts)

class WordCloud extends Component {
    constructor(props) {
        super(props);
        autobind(this);
    }

    render() {
        const { keywords } = this.props
        console.log(keywords);
        let words = []
        keywords.map(data => {
            let obj = {}
            obj.name = data.word
            if(data.count < 100) {
                obj.weight = 5
            } else {
                obj.weight = 6
            }
            words.push(obj)
        })

        let config = {
                chart: {
                    type: 'column',
                    inverted: false,
                    height:400,
                    marginTop:75,
                    marginBottom: 20,
                    borderRadius: 8,
                    backgroundColor: "#2B2E4A",
                },
                tooltip: {
                    enabled: false
                },
                series: [{
                    type: 'wordcloud',
                    data: words,
                    name: 'Occurrences',

                }],
                title: {
                    text: 'SENTIMENTAL WORDCLOUD',
                    y: 40,
                    style: {
                        color: '#ADB0D0',
                        fontFamily: 'Montserrat'
                    }
                },
                plotOptions: {
                    series: {
                        cursor: 'pointer',
                        events: {
                            click: function(event) {
                                let keyword = event.point.name
                                axios.all([
                                    axios.get(`/api/v1/tweets/@36,-115,7z?${keyword}`),
                                    axios.get(`/api/v1/tweets/@36,-115,7z/sentiments?keyword=${keyword}`)

                                ])
                                .then(axios.spread((tweets, sentiments) => {
                                    console.log(tweets);
                                    this.props.dispatch(handleKeywordTweets())
                                    console.log(sentiments);
                                }))
                                .catch(function(error){
                                    console.log(error);
                                })
                            }
                        }
                    }
                }
        }
        return (
            <ReactHighcharts config = {config}
            style={{ "min-width": "310px", "max-width": "800px", margin:" 0 auto"}}
            ></ReactHighcharts>
        );
    }
}

const mapStateToProps = (state) => {
    const { keywords } = state.places
    return { keywords }
}

export default connect(mapStateToProps)(WordCloud)
thetylerreiff commented 6 years ago

I believe this is refering to the chart. I would assign the expression to a var in the render call then use that to call the expression.

render() {
  let dispatch = this.props.dispatch

...

  .then(axios.spread((tweets, sentiments) => {
    ...
    dispatch(handleKeywordTweets())
}))

...
}
ptchiangchloe commented 6 years ago

Hi @reiff12 ,

Yes, I believe so too. There is another way to do which is use arrow function for onClick property, since arrow function doesn't have its own this value.

click: (event) => { 
...
   .then(axios.spread((tweets, sentiments) => {
...
    this.props.dispatch(handleKeywordTweets())
  }))
...
}

Thanks,

Han

kirjs commented 5 years ago

Closing and there has been no activity in this issue for a while. if this is still relevant, please feel free to reopen