javascript - React JS view not re-rendering on setState() - Stack Overflow

Using react js 0.13.1 and es6 with babel:I have a file input and a textarea, I want the user to be able

Using react js 0.13.1 and es6 with babel:

I have a file input and a textarea, I want the user to be able to select text files and have the text append to the textarea.

When the onChange event fires, it uses the FileReader API to read the file as text, then calls setState({text: <text from the file>}). That's working fine.

The problem is that when you select and open a file, nothing happens to the text in the textarea... it just keeps whatever text it was initialized with. It seems like react either isn't updating the view after setState(), or maybe I just misspelled something. Not sure yet, but any help is appreciated!

here's my (simplified) code:

'use strict';

class TextApp extends React.Component {
  constructor() {
    this.state = {
      text: 'wow'
    };
  }

  readFile(e) {
    var self = this;
    var files = e.target.files;

    for (var i = 0, len = files.length; i < len; i++) {
      var reader = new FileReader();

      reader.onload = function(upload) {
        var textState = (self.state.text || '') + upload.target.result;
        self.setState({
          text: textState
        });
      };
      reader.readAsText(files[i]);
    }
  }

  render() {
    return (
      <div>
        <TextInput text={this.state.text} />
        <FileInput onChange={this.readFile} />
      </div>
    );
  }
}

class TextInput extends React.Component {
  render() {
    return (
      <textarea>{this.props.text}</textarea>
    );
  }
}

class FileInput extends React.Component {
  render() {
    return (
      <div>
          <input type="file" onChange={this.props.onChange} multiple />
      </div>
    );
  }
}

React.render(<TextApp />, document.getElementById('reappct'));

Using react js 0.13.1 and es6 with babel:

I have a file input and a textarea, I want the user to be able to select text files and have the text append to the textarea.

When the onChange event fires, it uses the FileReader API to read the file as text, then calls setState({text: <text from the file>}). That's working fine.

The problem is that when you select and open a file, nothing happens to the text in the textarea... it just keeps whatever text it was initialized with. It seems like react either isn't updating the view after setState(), or maybe I just misspelled something. Not sure yet, but any help is appreciated!

here's my (simplified) code:

'use strict';

class TextApp extends React.Component {
  constructor() {
    this.state = {
      text: 'wow'
    };
  }

  readFile(e) {
    var self = this;
    var files = e.target.files;

    for (var i = 0, len = files.length; i < len; i++) {
      var reader = new FileReader();

      reader.onload = function(upload) {
        var textState = (self.state.text || '') + upload.target.result;
        self.setState({
          text: textState
        });
      };
      reader.readAsText(files[i]);
    }
  }

  render() {
    return (
      <div>
        <TextInput text={this.state.text} />
        <FileInput onChange={this.readFile} />
      </div>
    );
  }
}

class TextInput extends React.Component {
  render() {
    return (
      <textarea>{this.props.text}</textarea>
    );
  }
}

class FileInput extends React.Component {
  render() {
    return (
      <div>
          <input type="file" onChange={this.props.onChange} multiple />
      </div>
    );
  }
}

React.render(<TextApp />, document.getElementById('reappct'));
Share Improve this question edited Apr 23, 2015 at 9:04 Bergi 667k161 gold badges1k silver badges1.5k bronze badges asked Apr 23, 2015 at 1:55 mmmmmm 2,2973 gold badges27 silver badges44 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Along with what @BinaryMuse has suggested, you also have to bind readFile method like this

<FileInput onChange={this.readFile.bind(this)} />

Here is the updated demo

Use <textarea value={this.props.text} />. See Why Textarea Value?:

If you do decide to use children, they will behave like defaultValue.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745369354a4624728.html

相关推荐

  • javascript - React JS view not re-rendering on setState() - Stack Overflow

    Using react js 0.13.1 and es6 with babel:I have a file input and a textarea, I want the user to be able

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信