Skip to content

Commit

Permalink
update callback setstate mixup with ref
Browse files Browse the repository at this point in the history
  • Loading branch information
softmarshmallow committed Dec 15, 2021
1 parent 2b2a570 commit 4ef5ba0
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions editor/scaffolds/editor/editor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useCallback, useEffect, useRef, useState } from "react";
import styled from "@emotion/styled";
import { useRouter } from "next/router";
import { DefaultEditorWorkspaceLayout } from "layouts/default-editor-workspace-layout";
Expand Down Expand Up @@ -67,6 +67,10 @@ export function Editor() {
const targetted =
find_node_by_id_under_entry(targetId, root?.entry) ?? root?.entry;

const targetStateRef = useRef();
//@ts-ignore
targetStateRef.current = targetted;

useEffect(() => {
// ------------------------------------------------------------
// other platforms are not supported yet
Expand All @@ -88,6 +92,20 @@ export function Editor() {
// ------------------------------------------------------------
}, [state.design?.key, fat.accessToken]);

const on_result = (result: Result) => {
//@ts-ignore
if (result.id == targetStateRef?.current?.id) {
setResult(result);
}
};

const on_preview_result = (result: Result) => {
//@ts-ignore
if (result.id == targetStateRef?.current?.id) {
setPreview(result);
}
};

useEffect(() => {
const __target = targetted;
if (__target && framework_config) {
Expand All @@ -102,12 +120,6 @@ export function Editor() {
disable_components: !enable_components,
};

const on_result = (result: Result) => {
if (result.id == targetted.id) {
setResult(result);
}
};

// build code without assets fetch
designToCode({
input: _input,
Expand Down Expand Up @@ -140,12 +152,6 @@ export function Editor() {
disable_components: true,
};

const on_preview_result = (result: Result) => {
if (result.id == targetId) {
setPreview(result);
}
};

// ----- for preview -----
designToCode({
input: _input,
Expand Down

1 comment on commit 4ef5ba0

@vercel
Copy link

@vercel vercel bot commented on 4ef5ba0 Dec 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.