在 monaco editor action 运行 函数中访问当前的 redux 状态

accessing current redux state in monaco editor action run function

我有一个 monaco 编辑器实例,我为 Ctrl+S 键添加了一个动作。

const addActions = (editor: editor.IStandaloneCodeEditor) => {
        editor.addAction({
            id: "save-element-action",
            label: "Save Element",
            keybindings: [KeyMod.CtrlCmd | KeyCode.KEY_S],
            precondition: undefined,
            keybindingContext: undefined,
            contextMenuGroupId: "navigation",
            contextMenuOrder: 1.5,

            // @param editor The editor instance is passed in as a convinience
            run: function (ed) {
                // TODO: saveElement is called but doesnt work yet because expressionEditorState is empty (Closure??)
                saveElement();
            },
        });
    };

在 saveElement 函数中我想从我的 redux store 访问一些状态

    const saveElement = (editor?: editor.ICodeEditor) => {
        if (expressionEditorState.elementContent === undefined || expressionEditorState.elementContent === "") {
            dispatch(createErrorAction("No content for element"));
            return;
        }


        dispatch(createSaveElementAction(user, expressionEditorState.elementContent, expressionEditorState.elementMetaData));
    };

expressionEditorState 与初始状态相同(如此空)。 使用自定义挂钩和 useSelector 挂钩设置状态。

如何在摩纳哥动作的 运行 函数中访问 redux 的当前状态?

如果有人跟进... 解决方案是使用 thunks(参见 redux-thunks)。所以我将 saveElement 重写为 thunk 并发送它。

dispatch(saveElement())