项目A react 版本 ^15.1.0
包裹组件,为子组件添加通用功能
需求:图片加载完成前,使用默认图片
遇到问题: 无法给reactObject添加props
//参数1 需要复制的reactObject type => reactObject
//参数2 需要添加的props type => object
React.cloneElement(child, {
onClick: () => {
},
testProps: 'aaa'
})
实例
//父组件
<LoadImg>
<img src={
item.room_src_max} alt={
item.room_name} width="230" height="130"/>
</LoadImg>
//子组件
import React, {
PropTypes, Component } from 'react'
const noDefaultImg = 'url'
/**
* @param defaultImg string => url 默认图片
* @param content object => reactEl 渲染img
* */
class LoadImg extends Component {
constructor(props) {
super(props);
}
imgEl = null;
renderEl = () => {
const {
children } = this