Slate 提供了一个KeyUtils.setGenerator(myKeygenFunction)
传递我们自己的密钥生成器。这使我们有机会在 Editor 实例中创建真正唯一的键。
在导入此组件的父级中,idFromParentIteration
为每个PlainText
组件实例传递不同的道具,您应该会很好。像这样:
['first-editor', 'second-editor'].map((name, idx) => <PlainText idFromParentIteration={name + idx} />)
这是一个带有自定义密钥生成器的完整示例。
import React from "react";
import Plain from "slate-plain-serializer";
import { KeyUtils } from 'slate';
import { Editor } from "slate-react";
const initialValue = Plain.deserialize(
"This is editable plain text, just like a <textarea>!"
);
class PlainText extends React.Component {
constructor(props) {
super(props);
let key = 0;
const keygen = () => {
key += 1;
return props.idFromParentIteration + key; // custom keys
};
KeyUtils.setGenerator(keygen);
}
render() {
return (
<Editor
placeholder="Enter some plain text..."
defaultValue={initialValue}
/>
);
}
}
export default PlainText;