Javascript &引用;n、 createFromText不是函数;尝试用文本初始化编辑器时出错
在向用户显示文本之前,试图截断编辑器中的文本,但出现以下错误:, 请仔细看一看,你可能会发现我遗漏了什么Javascript &引用;n、 createFromText不是函数;尝试用文本初始化编辑器时出错,javascript,reactjs,draftjs,draft-js-plugins,Javascript,Reactjs,Draftjs,Draft Js Plugins,在向用户显示文本之前,试图截断编辑器中的文本,但出现以下错误:, 请仔细看一看,你可能会发现我遗漏了什么 main.js:46 TypeError: n.createFromText is not a function 我的代码 const truncate = (editorState, charCount) =>{ // charCount is a number const contentState = editorState.getCu
main.js:46 TypeError: n.createFromText is not a function
我的代码
const truncate = (editorState, charCount) =>{ // charCount is a number
const contentState = editorState.getCurrentContent();
const blocks = contentState.getBlockMap();
let count = 0;
let isTruncated = false;
const truncatedBlocks = [];
blocks.forEach((block) => {
if (!isTruncated) {
const length = block.getLength();
if (count + length > charCount) {
isTruncated = true;
const truncatedText = block.getText().slice(0, charCount - count);
console.log(contentState)
const state = contentState.createFromText(`${truncatedText}...`); // the error is around here
truncatedBlocks.push(state.getFirstBlock());
} else {
truncatedBlocks.push(block);
}
count += length + 1;
}
});
if (isTruncated) {
const state = contentState.createFromBlockArray(truncatedBlocks);
return EditorState.createWithContent(state);
}
return editorState;
}
const ContentState = convertFromRaw(JSON.parse(this.props.description));
const editorState = EditorState.createWithContent(ContentState);
const TrancatedText = this.props.charCount ? truncate(editorState, this.props.charCount): editorState