Your solution will certainly work, but the problems that you cause are not well suited for working with reduction. Just using simple React and passing the necessary functions to your component sounds a lot more natural to me.
In the case of export, for example, instead of sending an action that updates some state, which then launches a new window to open, why not just open a new window instead of sending this action? If you have the information necessary to send an action to start opening a window, you should simply open the window in the same place.
In the example, when clicking an error message calls a non-React, imperative api call, pass the function from the nearest common parent of the error message and the editor. The parent can also maintain a wrapper link around the editor. Even if itβs several levels in depth, itβs not so bad to get a link to what you want if you miss the function to set ref . Thus, the function passed from the parent to the component of the error message can simply call the method in ref, which it supports the editor. Basically, something like this:
class Parent extends Component { constructor(...args) { super(...args) this.onErrorMessageClick = this.onErrorMessageClick.bind(this) } onErrorMessageClick(lineNumber) { if (this.editor) { this.editor.focusOnLine(lineNumber) } } render() { return ( <div> <ErrorMessage onClick={ this.onErrorMessageClick } lineNumber={ 1 } /> <ErrorMessage onClick={ this.onErrorMessageClick } lineNumber={ 2 } /> <EditorWrapper editorRef={ (editor) => { this.editor = editor } } /> </div> ) } } const ErrorMessage = ({ onClick, lineNumber }) => ( <button onClick={ () => onClick(lineNumber) }> { `Error Message For ${lineNumber}` } </button> )
source share