I have a component which displays info supplied in props.
Is there a way to programmatically navigate to this component and pass to him jsx code?
With simple string (not jsx) I would do something like the following:
this.props.history.push("/mycomponent", { message: "Just a text" });
and in target component:
const {message} = this.props.location.state;
However, if I include in message
any jsx node, ie. Smiley <i className="far fa-smile"/>
it displays as plain text.
Is there any way to achieve this other than using dangerouslySetInnerHTML
? If not is it safe to use it in this context?
class MyComponent extends Component {
returnMessage = () => {
// this.props.location.state.message = 'Smiley <i className="far fa-smile"/>'
return {__html: this.props.location.state.message};
}
render() {
return <div dangerouslySetInnerHTML={returnMessage()} />;
}
}
export default MyComponent;