Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.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中不起作用_Javascript_Reactjs - Fatal编程技术网

Javascript 调用上下文函数在React中不起作用

Javascript 调用上下文函数在React中不起作用,javascript,reactjs,Javascript,Reactjs,我正在尝试从子组件更新上下文的状态,但未调用上下文函数 设置场景。下面是在Dashboard.jsx中传递给HandleModel的一些示例片段数据 { _id: "123", name: "test name", details: test details, content: "test content" } SnippetContext.js 应该从Dashboard.jsx中调用handleSnippetUpdate函数不是这样。 state = { name: ''

我正在尝试从子组件更新上下文的状态,但未调用上下文函数

设置场景。下面是在Dashboard.jsx中传递给HandleModel的一些示例片段数据

{
_id: "123", 
name: "test name", 
details: test details, 
content: "test content"
}

SnippetContext.js 应该从Dashboard.jsx中调用
handleSnippetUpdate
函数不是这样。

  state = {
    name: '',
    details: '',
    content: '',
  }

  handleSnippetUpdate = edit => event => {
    console.log('invoked') //does not get invoked
    this.setState({
      name: edit.name,
      details: edit.details,
      content: edit.content,
    })
  }

Dashboard.jsx 按钮调用HandleModel并传入代码段数据。

this.handleModal(snippet)}>Edit


handleSnippetUpdate
中删除
event=>
已修复该问题。 正如HMR和trixn在评论中指出的那样

 handleSnippetUpdate = edit => event => {
    console.log('invoked') //does not get invoked
    this.setState({
      name: edit.name,
      details: edit.details,
      content: edit.content,
    })
  }

handleSnippetUpdate(something)返回一个期望传递事件的函数,当您传递该事件时,该函数将设置状态处理程序定义中的
event=>
与您使用它的方式无关。它可能只需要将
edit
作为参数。它返回一个函数,该函数接受@HMR已经指出的
事件。所以它实际上创建了一个处理程序,而不是处理一些东西。
 handleSnippetUpdate = edit => event => {
    console.log('invoked') //does not get invoked
    this.setState({
      name: edit.name,
      details: edit.details,
      content: edit.content,
    })
  }