S
SolidJS15mo ago
Dikiy

how to make two-way data transfer through components?

I need to pass a value from a child component to a parent component and back again and idk how to make it
10 Replies
Jasmin
Jasmin15mo ago
sounds like a good use case for contexts. https://docs.solidjs.com/concepts/context
Dikiy
DikiyOP15mo ago
doesn't fit
Jasmin
Jasmin15mo ago
Why?
Dikiy
DikiyOP15mo ago
I need to send it through props without context It's possible in React, but it's not possible here doesn't fit the project structure I'd like at least a crutch variant
high1
high115mo ago
How do you mean through props? Props are sent from the parent to the child component But those props could be signals, i.e. you could send signal setters to the child If you don't want context - or stores
Dikiy
DikiyOP15mo ago
Why can't props be a function?
high1
high115mo ago
they can but the easiest way to have a change in child component visible in parent is to forward something that is already reactive and that's either a setter, or a shared store, context, even a function wrapper around something that's reactive
Grahf
Grahf15mo ago
const [value, setValue] = createSignal()

return (
<ChildComponent setValue={setValue} />
)
const [value, setValue] = createSignal()

return (
<ChildComponent setValue={setValue} />
)
Then in the ChildComponent you just do:
props.setValue(5)
props.setValue(5)
And it will change in the parent
high1
high115mo ago
As I said before, you could send signal setters to the child. That's that.
Dikiy
DikiyOP15mo ago
it's working thanks

Did you find this page helpful?