我尝试使用redux使我的系列数据具有click事件,但是,当我尝试在回调中调度我的函数时遇到问题,我使用了React-Highcharts库,我也尝试过在组件安装后访问图表,但我不知道如何做到这一点,因为没有示例。
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)
答案 0 :(得分:2)
请注意,您使用的是非箭头表示法功能作为点击处理程序:
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);
})
}
通过使用非箭头符号,该函数定义了自己的&#34;这个&#34;值。 但是,箭头功能并没有自己的&#34;这个&#34;值,但它使用封闭执行上下文的值(在您的情况下,&#34;此&#34;指的是React类WordCloud)。
长话短说,尝试将处理程序转换为箭头符号,并尝试始终使用箭头符号,因为之前的符号几乎已过时:)