What I want to do is when I type some text in an input field, it should appear in another place realtime.
Below is my input;
This could be achieved with a hook. However, I would not recommend it, as it strictly couples state and layout.
const useInput = (placeholder, initial) => {
const [value, setVal] = useState(initial)
const onChange = (e) => setVal(e.target.value)
const element =
return {element, value}
}
Use it in any functional component
const BensPlayGround = () => {
const name = useInput("Enter name here")
return (
<>
{name.element}
Hello {name.value}
>
)
}
const useDataBind = () => {
const [value, setVal] = useState("")
const onChange = (e) => setVal(e.target.value)
return {value, onChange}
}
const Demo = (props) => {
const nameProps = useDataBind()
return (
<>
Hello {nameProps.value}
>
)
}