Cute Bow Tie Hearts Blinking Pink Pointer

프론트엔드/React

[React] 타자 입력 시마다 value값 변하는 form만들기

청포도 에이드 2022. 4. 14. 17:22
728x90

 

기본세팅

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
</head>
<body>
    <div id="root"></div>
    <script type="text/babel">


        class App extends React.Component{
            render(){
                return(
                    <div>
                       
                    </div>    
                )
            }
        }


        ReactDOM.render(
            <App />,
            document.querySelector('#root')
        )
        
    </script>
</body>
</html>

 

 

 

    <div id="root"></div>
    <script type="text/babel">

        class Form extends React.Component{
            handleChange = e => {
                const {target:{value}} = e
                this.props.change(value)
            }

            handleSubmit = e => {
                e.preventDefault()
            }

            render(){
                const {
                    props:{
                        value
                    },
                    handleChange,
                    handleSubmit
                } = this

                return(
                    <form onSubmit={handleSubmit}>
                        내용 : <input type="text" value={value} onChange={handleChange} />
                        <input type="submit" value="확인" />
                    </form>
                )
            }
        }

        class Value extends React.Component{
            render(){
                return(
                    <h3>{this.props.value}</h3>
                )
            }
        }


        // React Devloper Tool

        class App extends React.Component{

            state = {
                value:''
            }

            changeValue = v => {
                this.setState({
                    ...this.state,
                    value:v
                })
            }

            
            render(){
                return(
                    <div>
                        <Form change={this.changeValue} value={this.state.value}/>
                        <Value value={this.state.value} />
                    </div>
                )
            }
        }

        ReactDOM.render(
            <App />,
            document.querySelector('#root')
        )
    </script>
</body>
728x90