Skip to content

Input and hooks #1142

Closed Answered by MarcusNotheis
kbachl asked this question in Q&A
Jan 8, 2021 · 1 comments · 8 replies
Discussion options

You must be logged in to vote

Hey @kbachl,

it should be almost the same with our Input component: you can access the current value via event.target.value:

const MyComponent = () => {
  const [value, setValue] = useState("");

  const onInputChange = useCallback(
    (event) => {
      setValue(event.target.value);
    },
    [setValue]
  );

  return <Input value={value} onInput={onInputChange} />;
};

This sample would update the value on every keystroke in the input, you can use onChange if you want to update it only once after the input looses focus.

You can also omit the useCallback if you want to, that's just a performance optimization.

I hope this helps 🙂

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@MarcusNotheis
Comment options

@kbachl
Comment options

@MarcusNotheis
Comment options

@kbachl
Comment options

@kbachl
Comment options

Answer selected by MarcusNotheis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants