Refactor and shrink effect code

This commit is contained in:
Sam Nolan 2022-05-02 20:52:55 +00:00
parent 47b67a9efb
commit 5fc829efc9

View File

@ -166,10 +166,9 @@ export let SquigglePartial: React.FC<SquigglePartialProps> = ({
let [expression, setExpression] = React.useState(initialSquiggleString); let [expression, setExpression] = React.useState(initialSquiggleString);
let [error, setError] = React.useState<string | null>(null); let [error, setError] = React.useState<string | null>(null);
// Runs squiggle and updates state/calls props apprropriately React.useEffect(() => {
let runSquiggle = (newExpression: string) => {
let squiggleResult = runPartial( let squiggleResult = runPartial(
newExpression, expression,
bindings, bindings,
samplingInputs, samplingInputs,
jsImports jsImports
@ -180,21 +179,14 @@ export let SquigglePartial: React.FC<SquigglePartialProps> = ({
} else { } else {
setError(errorValueToString(squiggleResult.value)); setError(errorValueToString(squiggleResult.value));
} }
}; }, [expression]);
// Run this once on mount, so that the next cells can get relevent values
React.useEffect(() => runSquiggle(expression), []);
return ( return (
<div> <div>
<Input> <Input>
<CodeEditor <CodeEditor
value={expression} value={expression}
onChange={(x) => { onChange={setExpression}
// When the code changes, rerun squiggle and inform next cells
setExpression(x);
runSquiggle(x);
}}
oneLine={true} oneLine={true}
showGutter={false} showGutter={false}
height={20} height={20}