Data binding in React

前端 未结 12 771
心在旅途
心在旅途 2021-01-30 19:49

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;



        
12条回答
  •  既然无缘
    2021-01-30 20:40

    With the new feature called Hooks from the React team which makes functional components to handle state changes.. your question can be solved easily

    import React, { useState, useEffect } from 'react'
    import ReactDOM from 'react-dom';
    
    const Demo = props =>{
        const [text, setText] = useState("there");
        return props.logic(text, setText);
    };
    
    const App = () => {
        const [text, setText] = useState("hello");
    
        const componentDidMount = () =>{
            setText("hey");
        };
        useEffect(componentDidMount, []);
    
        const logic = (word, setWord) => (
            

    {word}

    setWord(e.target.value)}>

    {text}

    setText(e.target.value)}>
    ); return ; }; ReactDOM.render(,document.getElementById("root"));

提交回复
热议问题