Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将更多参数传递给React组件上的onMessage函数_Javascript_React Redux - Fatal编程技术网

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)