在这里,我无法从父级渲染子级组件ModalConatiner。但是,我想提一下,在这之前和之后都有一串父子组件。我也已经导入了它。
getAssetDocId(e) {
var assetDocId = e.currentTarget.attributes['id'].value;
var assetDBId = e.currentTarget.attributes['dbId'].value;
var code = e.currentTarget.attributes['code'].value;
console.log('----------------------------------------------------');
return <ModalContainer assetDocId={assetDocId} assetDBId={assetDBId} code={code} defaultValues={this.props.defaultValues} allAssetDetails={this.props.allAssetDetails} />
}
由
调用<div className="card-overlay modal-trigger" data-target="asset-detail" id={asset.docId} dbId={asset.dbId} code={asset.code} key={asset.docId} onClick={this.getAssetDocId}>
子组件-
import React, { Component } from 'react';
import {Button, Icon, Row, Col, Carousel, Chip, Collapsible, CollapsibleItem, Input, Dropdown, NavItem, Form, Modal} from 'react-materialize';
import AssetDetails from './../assetlisting/assetdetails.jsx';
class ModalContainer extends Component {
constructor(props) {
super(props);
this.resetValues = this.resetValues.bind(this);
this.resetAssetValues = React.createRef();
this.renderAssetDetails = this.renderAssetDetails.bind(this);
}
renderAssetDetails() {
return <AssetDetails assetDocId={this.props.assetDocId} assetDBId={this.props.assetDBId} code={this.props.code} defaultValues = {this.props.defaultValues} ref={this.resetAssetValues} allAssetDetails={this.props.allAssetDetails}/>;
}
resetValues = () => {
this.resetAssetValues.current.showInfo();
};
render() {
console.log('ppppppppppppppppppppppppppppppp');
return (
<div>
<Modal id="asset-detail" className="fullscreen"
modalOptions={{preventScrolling: true}}>
<div className="modal-content">
<div className="asset-details">
<div className="modal-close waves-effect" onClick={this.resetValues}>
<i className="material-icons">close</i>
</div>
{ this.renderAssetDetails() }
</div>
</div>
</Modal>
</div>
)
}
}
export default ModalContainer;
有人可以指出这里的问题吗?
答案 0 :(得分:2)
您需要做的是呈现子组件
检查以下代码,以更好地理解。这就是实现功能的方法
constructor(props){
super(props);
this.state = {
assetDocId: "",
assetDBId: "",
code: "",
showModalContainer: false
}
}
getAssetDocId = e => {
this.setState({
assetDocId:e.currentTarget.attributes['id'].value,
assetDBId: e.currentTarget.attributes['dbId'].value,
code: e.currentTarget.attributes['code'].value,
showModalContainer: true
})
}
render(){
const {assetDocId, assetDBId, code, showModalContainer} = this.state;
return(
<div>
{showModalContainer && <ModalContainer assetDocId={assetDocId} assetDBId={assetDBId} code={code} defaultValues={this.props.defaultValues} allAssetDetails={this.props.allAssetDetails} />}
</div>
)
}
有很多方法可以做到这一点。
答案 1 :(得分:1)
onClick方法对返回的值不做任何事情,因为您正在从getAssetDocId
方法返回模态容器,所以它不起作用,您需要设置一个状态并根据以下内容渲染ModalContainer状态值
getAssetDocId(e) {
var assetDocId = e.currentTarget.attributes['id'].value;
var assetDBId = e.currentTarget.attributes['dbId'].value;
var code = e.currentTarget.attributes['code'].value;
this.setState({
assetDocId,
assetDBId,
code,
showModal: true
})
}
render() {
const { assetDocId, assetDBId, code, showModal } = this.state;
return (
<React.Fragment>
<div className="card-overlay modal-trigger" data-target="asset-detail" id={asset.docId} dbId={asset.dbId} code={asset.code} key={asset.docId} onClick={this.getAssetDocId}>
{showModal && <ModalContainer assetDocId={assetDocId} assetDBId={assetDBId} code={code} defaultValues={this.props.defaultValues} allAssetDetails={this.props.allAssetDetails} />}
</React.Fragment>
)
}