html – React中img标记中的onError

我想用react中的认图像替换断开的链接.我通常会使用onerror,但它没有按预期工作.具体来说,我得到了“在现有状态转换期间无法更新(例如在渲染中)”的重复错误.最终,出现认图像,但需要很长时间(此错误的许多打印).这是一个非常类似的问题: react.js Replace img src onerror.我尝试了这个解决方案(排名第一,不使用jQuery),但它导致了所描述的错误.我猜onError必须不断被触发,从而导致不断重新渲染.任何替代解决方案/修复?
import React from 'react';
import { connect } from 'react-redux';
//import AddImageModal from '../components/AddImageModal.js';
import Button from 'react-bootstrap/lib/Button';
//import { getPostsByUserId } from 'actions'
import Posts from '../components/Posts.js';
var Modal = require('react-modal');
require('../../styles/AddImageModal.scss');
import { save_post } from '../actions';

 const customStyles = {
  content : {
    top                   : '50%',left                  : '50%',right                 : 'auto',bottom                : 'auto',marginRight           : '-50%',transform             : 'translate(-50%,-50%)'
  }
};

var MyWallScreen = React.createClass({

  getinitialState: function() {
    return { 
      modalisOpen: false,imageUrl: "" 
    };
  },openModal: function() {
    this.setState({modalisOpen: true});
  },afterOpenModal: function() {
    // references are Now sync'd and can be accessed. 
    this.refs.subtitle.style.color = '#f00';
  },closeModal: function() {
    this.setState({modalisOpen: false});
  },setUrl: function(e,val) 
  {
    if (e.keyCode === 13) 
        {
          this.setState({
            imageUrl: val
          });
        }
  },resetimageUrl: function()
  {
    this.setState({
      imageUrl: ""
    });
  },onError: function() {
    this.setState({
      imageUrl: "default.jpg"
    });
  },render: function() {
    const { userPosts,dispatch } = this.props;
    return (
      <div>
        <button onClick={this.openModal}>Add Image</button>

        {/* The meat of the modal. */}
        <Modal
          isOpen={this.state.modalisOpen}
          onAfterOpen={this.afterOpenModal}
          onRequestClose={this.closeModal}
          style={customStyles} >

        <div className="modalBox">
          <h2 className="modalBanner">Add an image link</h2>
          <input ref="urlInput" 
                className="modalinput"
                onKeyDown={e=>this.setUrl(e,this.refs.urlInput.value)}/>
          {this.state.imageUrl ?
            <img className="modalimage" 
                  src={this.state.imageUrl}
                  onError={this.onError()}/>
            :<div className="modalimage"></div>
          }
          <div>
            <Button className="modalButton" bsstyle = "success" 
                onClick = {() => {
                dispatch(save_post(0,this.state.imageUrl));
                this.closeModal();
                this.resetimageUrl();
                }}>
                Post
            </Button>
            <Button className="modalButton" bsstyle = "danger" 
                onClick = {() => {
                  this.closeModal();
                  this.resetimageUrl();
                }}>
                Cancel
            </Button>
          </div>
        </div>
        </Modal>


        <Posts posts={userPosts}/>
      </div>
    );
  }
});


function mapStatetoProps(state,ownProps) {
  return {
    userPosts: state.posts[0]
  }
}


MyWallScreen = connect(mapStatetoProps)(MyWallScreen);

export default MyWallScreen;

解决方法

代码调用this.onError而不是传递对它的引用.每次调用render都会调用this.onError().删除括号,看看是否修复了它:
<img className="modalimage" 
  src={this.state.imageUrl}
  onError={this.onError()}/> // `onError` is being called here

固定版本:

<img className="modalimage" 
  src={this.state.imageUrl}
  onError={this.onError}/> // `onError` is being passed as a reference here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Mip是什么意思以及作用有哪些
怎么测试Mip页面运行情况
MIP安装的具体步骤有哪些
HTML添加超链接、锚点的方法及作用详解(附视频)
MIP的规则有哪些
Mip轮播图组件中的重要属性讲解
Mip的内联框架组件是什么
怎么创建初始的MIP配置及模板文件
HTML实现多选框及无法提交多数据的原因分析(附视频)
HTML如何设置复选框、单选框以及默认选项?(图文+视频)
怎么使用MIP组件
Div垂直居中效果怎么实现
HTML如何实现视频在线播放
如何使用Mip代码校验工具
Mip中弹出层组件是什么
如何用HTML实现简单按钮样式
Mip中快速回顶组件怎么用
Div内容居中效果如何实现
Div水平居中效果怎么实现
Mip中列表组件怎么用