【问题标题】:Uncaught Invariant Violation未捕获的不变违规
【发布时间】:2020-05-25 04:39:27
【问题描述】:

据我了解,react 子元素必须在父元素内。我是这样写的。并显示未捕获的不变违规。

render() {
 return (
  <div>
    <p>You {this.state.liked} this. Click to toggle.</p>
    <button onClick="this.increment">Increment</button>
  </div>
  );
 }

但是当我在 div 内的 click 事件上编写按钮时,它会显示预期的结果。

render() {
  return (
   <div onClick={this.increment}>
    <p>You {this.state.liked} this. Click to toggle.</p>
   </div>
  );
}

我在这里缺少什么?

【问题讨论】:

    标签: reactjs ecmascript-6


    【解决方案1】:

    在第一个示例中,您在 onClick 道具中传递了一个字符串,但 React 期望它是一个函数。

    【讨论】:

      【解决方案2】:

      这就是我的工作方式。

      import { DOM } from "react";
      
      return (
          DOM.div({
              className: "", onClick: () => {
                  executeMicroflow(props.microflowProps);
              },
              style: { width: Value + "%" }
          })
      );
      
      function executeMicroflow(params) {}
      

      【讨论】:

        【解决方案3】:

        当我在同名文件夹中有一个 TSX 文件时出现此错误。

        我将文件复制并粘贴到其他位置。因为文件夹存在。没有编译时错误。

        但在运行时,require 调用没有映射到文件。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-09
          • 2021-02-16
          • 2023-04-08
          • 2016-06-26
          • 1970-01-01
          相关资源
          最近更新 更多