我在 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?
答案 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>
);