类型错误:无法读取未定义的属性“clickNode”

时间:2021-04-10 17:12:16

标签: reactjs

我在 React 组件中创建了这两个函数。以下代码返回错误:

const UncontrolledDiagram = ({ sentence }) => {
  // create diagrams schema

  const [schema, { onChange, addNode, removeNode }] = useSchema(initialSchema);

  const clickNode = () => {
    console.log("Click event");
  }
  const BaseNode = ({ content }) => (
    <div className='button' style={{ width: '70px', fontSize: '0.6rem', textAlign: 'center' }}>
      <a onClick={this.clickNode}>
        <div role="button">
          {content}
        </div>
      </a>
    </div>
  );

如何从 BaseNode 中调用 clickNode

1 个答案:

答案 0 :(得分:0)

您无需在功能组件中编写“this”,因此您的代码应如下所示

const UncontrolledDiagram = ({ sentence }) => {
  // create diagrams schema

  const [schema, { onChange, addNode, removeNode }] = useSchema(initialSchema);

  const clickNode = () => {
    console.log("Click event");
  }
  const BaseNode = ({ content }) => (
    <div className='button' style={{ width: '70px', fontSize: '0.6rem', textAlign: 'center' }}>
      <a onClick={clickNode}>
        <div role="button">
          {content}
        </div>
      </a>
    </div>
  );