Uncontrolled component

For all input components, live and fetch as uncontrolled components, components that use ref more often

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="Width = device - width, initial - scale = 1.0">
    <title>Document</title>
</head>
<body>
    <! -- Get the container ready -->
    <div id="test"></div>
    <! Import dependencies, import dependencies, must follow this step -->
    <script type="text/javascript" src=".. /js/react.development.js"></script>
    <script type="text/javascript" src=".. /js/react-dom.development.js"></script>
    <script type="text/javascript" src=".. /js/babel.min.js"></script>
    
    <! -- JSX syntax is parsed with Babel
    <script type="text/babel">
        class Login extends React.Component{
            // Live is an uncontrolled component
            handleSubmit=() = >{
                event.preventDefault()
                console.log('this'.this)
                const {username,password} = this
                console.log('The username is${username.value}, the password is${password.value}`)}render(){
                return(
                    <form onSubmit={this.handleSubmit}>The user name<input ref={c= >This. username = c} type="text" name="username"/> Password<input ref={c= >this.password = c} type="password" name="password"/>
                        <button>The login</button>
                    </form>
                )
            }
        }

        ReactDOM.render(<Login/>.document.getElementById('test'))
    </script>
</body>
</html>
Copy the code

The controlled components

Ref has a performance disadvantage of recommending the use of controlled components, as input maintenance states are controlled components that generally use oChange events.

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="Width = device - width, initial - scale = 1.0">
    <title>Document</title>
</head>
<body>
    <! -- Get the container ready -->
    <div id="test"></div>
    <! Import dependencies, import dependencies, must follow this step -->
    <script type="text/javascript" src=".. /js/react.development.js"></script>
    <script type="text/javascript" src=".. /js/react-dom.development.js"></script>
    <script type="text/javascript" src=".. /js/babel.min.js"></script>
    
    <! -- JSX syntax is parsed with Babel
    <script type="text/babel">
        class Login extends React.Component{
            // Initialization state
            state={
                username:' '.password:' ',}// As input maintenance state is controlled component ref has performance defects recommend using controlled component
            handleSubmit=() = >{
                event.preventDefault()
                console.log('this'.this)
                const {username,password} = this.state
                console.log('The username is${username}, the password is${password}`)
            }
            userNameChange=(e) = >{
                console.log(e.target.value);
                this.setState({username:e.target.value})
            }
            passwordChange=(e) = >{
                console.log(e.target.value);
                this.setState({password:e.target.value})
            }
            render(){
                return(
                    <form onSubmit={this.handleSubmit}>The user name<input onChange={this.userNameChange} type="text" name="username"/>password<input onChange={this.passwordChange} type="password" name="password"/>
                        <button>The login</button>
                    </form>
                )
            }
        }

        ReactDOM.render(<Login/>.document.getElementById('test'))
    </script>
</body>
</html>
Copy the code