我正在尝试在我的项目中使用 PrimeReact 的编辑器组件
import React from 'react';
import Editor from './editor/Editor';
const header = (
<span className="ql-formats">
<button className="ql-bold" aria-label="Bold"></button>
<button className="ql-italic" aria-label="Italic"></button>
<button className="ql-underline" aria-label="Underline"></button>
</span>
);
function Admin() {
return(
<div className="Admin">
<header className="App-header">
<Editor style={{height:'320px'}} value={this.state.text.bind(this)} onTextChange={(e) => this.setState({text: e.htmlValue}).bind(this)} />
</header>
</div>
);
}
但是当我运行此代码时,出现以下错误
TypeError: this is undefined
最佳答案
您使用函数组件而不是类组件 请阅读Function component vs class component
Function component vs class component 2
对于快捷方式解决方案,请使用 useState
import React, {useState} from 'react'; //import useState
import Editor from './editor/Editor';
const header = (
<span className="ql-formats">
<button className="ql-bold" aria-label="Bold"></button>
<button className="ql-italic" aria-label="Italic"></button>
<button className="ql-underline" aria-label="Underline"></button>
</span>
);
function Admin() {
const [text,setText] = useState('')
return(
<div className="Admin">
<header className="App-header">
<Editor style={{height:'320px'}} value={text} onTextChange={(e) => setText({text: e.htmlValue}).bind(this)} />
</header>
</div>
);
}
关于javascript - TypeError 这是未定义的原因,使用 primereact,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59783688/