Closed bogdansoare closed 6 years ago
Can you provide a JSFiddle with your current code?
A working example can be seen here https://jsfiddle.net/conorcussell/g5q576Lq/1/ but feel free to jump in the slack channel and ask any questions
Here is a codesanbox https://codesandbox.io/s/9367264y3w
@bogdansoare here is a fixed version https://codesandbox.io/s/3kx5pxv7om
There were two issues:
<RichTextEditor
value={htmlSerializer.deserialize(this.state.value)}
onChange={value => this.setState({ value: htmlSerializer.serialize(value) })}
/>
state = {
value: htmlSerializer.deserialize("<p>Lorem <strong>ipsum</strong></p>")
};
...
<RichTextEditor
value={this.state.value}
onChange={value => this.setState({ value })}
/>
handleChange = ({ value }) => {
const { onChange } = this.props;
if (onChange) {
if (value.document !== this.props.value.document) {
onChange(value);
}
}
};
handleChange = ({ value }) => {
const { onChange } = this.props;
onChange(value);
};
Let me know if that makes sense?
@conorcussell thank very much for the explanation. The issue is that I want to store the value as Html instead of the internal state of slate. Any ideas on how to do that?
@bogdansoare here is a new fork to show you how https://codesandbox.io/s/ovrkx9o0z5.
I added a method to the parent component to handle changes, it conditionally serializes the value to html if the document has changed and updates the state. The state now stores both the full Slate value and a string respresentation of the html.
handleChange = value => {
let newState = {
...this.state,
value
};
if (value.document !== this.state.value.document) {
// handle a document change
newState = {
...newState,
html: htmlSerializer.serialize(value)
};
}
this.setState(newState);
};
...
<RichTextEditor
value={this.state.value}
onChange={this.handleChange}
/>
Closing this now in favour of Slack feel free to DM me if you have any more questions, otherwise questions on the general channel usually get answered quickly.
Could you please provide an example on how to create a controlled slate component which gets its value from the props rather than the state? I've tried it but on each change the editor was losing focus