Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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} />
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading