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

Time:09-29

I have a component with a createSignal call:

import SetsImage from "./setsImage";

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

return (
<div>
<UsesImage img={img()} />
<SetsImage setImg={setImg()} />
</div>)
}
// setsImage.jsx
let setsImage = (props) =>{

let setTheImage (input)=>{
props.setImg(input);
}
...
}

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?

CodePudding user response:

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} />
  • Related