Javascript 将更多参数传递给React组件上的onMessage函数
我有一个Javascript 将更多参数传递给React组件上的onMessage函数,javascript,react-redux,Javascript,React Redux,我有一个EventSource,它在我的React组件的componentDidMount中使用onMessage函数loadGames,由于结果是一个对象数组,我想传递一个参数以从该数组中找到一个特定对象,但我不能,因为当我定义onMessage时,我不会使用任何参数调用函数loadGames,即使它里面隐藏了事件参数 我曾尝试在使用该数组的componentDidMount中放入另一个函数,但它不能,因为它在更新MapStateTrops //on my component file
EventSource
,它在我的React
组件的componentDidMount
中使用onMessage
函数loadGames
,由于结果是一个对象数组,我想传递一个参数以从该数组中找到一个特定对象,但我不能,因为当我定义onMessage
时,我不会使用任何参数调用函数loadGames
,即使它里面隐藏了事件
参数
我曾尝试在使用该数组的componentDidMount
中放入另一个函数,但它不能,因为它在更新MapStateTrops
//on my component file
url = 'https://wheel-of-fortune-server.herokuapp.com'
source = new EventSource(`${this.url}/stream`)
componentDidMount() {
this.source.onmessage=this.props.loadGames
//if i write this.props.loadGames(event) i get a compilation error
}
//on my action file
export function loadGames (event){
//what i want to do is loadGames (event,gameId)
const {data}=event
const games=JSON.parse(data)
//so i can do const game=games.find(game=>game.id===gameId)
return {
type: LOAD_ALL_GAMES,
payload: games
}
}
您可以使onmessage处理程序成为一个调用loadGames的函数,其参数为:
this.source.onmessage=event=>this.props.loadGames(event)
。您可以使onmessage处理程序成为使用所需参数调用loadGames的函数:this.source.onmessage=event=>this.props.loadGames(event)代码>。