Search code examples
javascriptcomponentssettersolid-js

How do I pass a signal setter as a prop to a child component?


I have a component with a createSignal call:

import SetsImage from "./setsImage";

let App = () =>{
  let [img, setImg] = createSignal();

  return (
    <div>
      <UsesImage img={img()} />
      <SetsImage setImg={setImg()} />
    </div>
  );
}

This setImg call however returns undefined. I see in the tutorial on props that they are described as "read only". So how do I pass signal setters to child components?


Solution

  • You are getting undefined as while passing the setImg function as prop you are executing it and hence passing its return value as prop. Instead you need the pass the function directly:

     <SetsImage setImg={setImg} />