SolidJSS
SolidJSโ€ข2y agoโ€ข
7 replies
edygar

createSignal + createRenderEffect

Say I have an Input component that might or not be controlled. Within this said component, I need to peek reactively into the value. Is it a good idea to have something like the following?


function Input(props) {
  const [value, setValue] = createSignal<Exclude<typeof props["value"], undefined>>()
  createRenderEffect(() => {
    setValue(props.value)
  })
  
  createEffect(() => {
    doClientSideOnlyStuff(value())
  })

  return (
    <input
      // ... many other stuff
      value={value()}
      onInput={(e) => {
        const previous = value()
        // expose normally
        props.onInput?.(e)

        if (!e.defaultPrevented && value() === previous && previous !== e.target.value) {
          // this order matter, first locally
          setValue(e.target.value)
        }
      }}
    />
  )
}


This is also compatible with SSR, since the createRenderEffect will run on the server. My main doubt is for things like
Was this page helpful?