• TypeError: instance.render is not a function


    1、错误描述

    2、错误原因

    import React, {Component} from 'react';
    import {render} from 'react-dom';
    import Button from 'react-bootstrap/Button'
    
    class InputRead extends Component {
    	constructor() {
    		return { user: '' };
    	}
    	
    	inputChange(e) {
    		this.setState({user:e.target.value});
    	}
    	
    	clearAndFocus() {
    		this.setState({user:''},()=>{
    			this.refs.thisInput.focus();
    		})
    	}
    	
    	render() {
    		return (
    			<div>
    				<Button onClick={this.clearAndFocus.bind(this)}>
    					清空
    				</Button>
    				<input ref="thisInput" value={this.state.user} onChange={this.inputChange.bind(this)}/>
    			</div>
    		)
    	}
    }
    
    export default InputRead;

      构造函数中不能使用return语句,否则以为构造函数中需要添加render()方法

    3、解决办法

    import React, {Component} from 'react';
    import {render} from 'react-dom';
    import Button from 'react-bootstrap/Button'
    
    class InputRead extends Component {
    	constructor(props) {
    		//return { user: '' };
    		super(props);
    		this.state = { user: '' };
    	}
    	
    	inputChange(e) {
    		this.setState({user:e.target.value});
    	}
    	
    	clearAndFocus() {
    		this.setState({user:''},()=>{
    			this.refs.thisInput.focus();
    		})
    	}
    	
    	render() {
    		return (
    			<div>
    				<Button onClick={this.clearAndFocus.bind(this)}>
    					清空
    				</Button>
    				<input ref="thisInput" value={this.state.user} onChange={this.inputChange.bind(this)}/>
    			</div>
    		)
    	}
    }
    
    export default InputRead;
  • 相关阅读:
    超详细教程2021新版oracle官网下载Windows JAVA-jdk11并安装配置(其他版本流程相同)
    个人总结
    6.15 团队项目心得
    五月团队项目收获
    八大排序算法读书笔记
    设计模式读书笔记3
    设计模式读书笔记2
    结对编程收获
    设计模式读书笔记
    UI-12组结对编程作业总结
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13313710.html
Copyright © 2020-2023  润新知