SolidJSS
SolidJS2y ago
24 replies
Vexcited

Is it bad to have A LOT of `createEffect` in a single component ?

Hello !

In my use case, I'm doing components to bridge classes from JavaFX to SolidJS.

The classes are implemented in a way that you have a setX and getX for each property.
If you setX multiple times the same object, it will reassign it and re-run everything on the JavaFX side.

That's why I don't want to do everything in a single
createEffect
, to prevent computations in case the object assigned to X property is the same as before.

For now, my code looks like this :
const StageContext: FlowComponent<{
  instance: Stage
  title: string
  show?: boolean
}> = (props) => {
  createEffect(() => {
    if (props.show) props.instance.show();
    else props.instance.hide();
  })

  createEffect(() => props.instance.setTitle(props.title));

  createEffect(() => {
    console.info("set children to scene");
    if (props.children instanceof Scene) props.instance.setScene(props.children);
    else throw new Error("The children of a StageContext must be a Scene instance.");
  });

  return props.instance;
};


Here, there's only three properties so three
createEffect
, but you can easily guess how many
createEffect
there'll be for more bigger components with more than 10 different properties.

So is it bad to have a lot of
createEffect
in a component ?
Are there any alternative to do this that also prevents running twice a computation that was already done ?
Was this page helpful?