input's event.target is null within this.setState [React.js]

前端 未结 5 1871
暖寄归人
暖寄归人 2020-12-17 09:42

In my react component I have a file input:

` 

and my onFileChan

5条回答
  •  星月不相逢
    2020-12-17 10:22

    Very simple/basic example to do the same task:

    class Hello extends React.Component {
        constructor(props) {
        super(props);
        this.state = {
        file: ''
        };
      }
    
      render() {
        return 
    { this.setState({file: e.target.files[0]}, () => { console.log('state', this.state); }) }} />
    ; } } ReactDOM.render( , document.getElementById('container') );

    I have added console log when the state will be set it will log the file details. You can see in the log when you select a file the state includes the file data.

    To see the console log you need to right click and inspect and see the Console.

    Checkout working example here https://jsfiddle.net/1oj3h417/2/

    Let me know if you have any doubt

提交回复
热议问题