Javascript 警告:使用Reactjs无法访问代码
我正在使用ReactJs。我有两个组件,PrescriptionIndex和PrescriptionNew,它们相互集成 这是我的第一部分“处方新”Javascript 警告:使用Reactjs无法访问代码,javascript,reactjs,material-ui,Javascript,Reactjs,Material Ui,我正在使用ReactJs。我有两个组件,PrescriptionIndex和PrescriptionNew,它们相互集成 这是我的第一部分“处方新” import React,{Component}来自'React'; 从“物料ui/FloatingActionButton”导入FloatingActionButton; 从“材质ui/svg图标/content/add”导入ContentAdd; 从“材质ui/样式/MuiThemeProvider”导入MuiThemeProvider 类P
import React,{Component}来自'React';
从“物料ui/FloatingActionButton”导入FloatingActionButton;
从“材质ui/svg图标/content/add”导入ContentAdd;
从“材质ui/样式/MuiThemeProvider”导入MuiThemeProvider
类PrescriptionNew扩展了组件{
render(){
返回(
)
}
}
导出默认处方new代码>您的代码当前为
if (X)
return A
else
return B
return C
当然,C
在这里是无法访问的。我想您的意思是放弃else案例(B
),您当前返回的是null
,而不是C
if (this.props.prescriptions.prescriptions !== undefined) {
return this.props.prescriptions.prescriptions.map(function(data){
return (
<div className="image-prescription" key={data.id}>
<Link to={"/update/" + data.id} >
<img src={data.image_path.image_path.url}
className="prescription-image"
alt="prescription"
/>
</Link>
</div>
);
});
} else {
// return null; <== remove this
return (
<div>
<PrescriptionNew />
</div>
)
}
if(this.props.prescriptions.prescriptions!==未定义){
返回此.props.prescriptions.prescriptions.map(函数(数据){
返回(
);
});
}否则{
//returnnull;原因很简单,您有一个if-else,并且您从这两个返回,因此代码的最后一部分无法访问
你可能想要这个
import React , { Component } from 'react';
import { connect } from "react-redux";
import { fetchPrescriptionFromUrl } from '../actions/index.js';
import { Link } from 'react-router';
import PrescriptionNew from './new.jsx';
import '../app.css';
class PrescriptionIndex extends Component {
componentDidMount(){
this.props.fetchData(PORT+'/prescriptions');
}
render() {
if (this.props.has_error){
return(<p> Fetching an Api results in error</p>)
}
if (this.props.has_loading){
return(<p> Loading...</p>)
}
return(
<div>
<PrescriptionNew />
{this.props.prescriptions.prescriptions && this.props.prescriptions.prescriptions.map(function(data){
return (
<div className="image-prescription" key={data.id}>
<Link to={"/update/" + data.id} >
<img src={data.image_path.image_path.url}
className="prescription-image"
alt="prescription"
/>
</Link>
</div>
);
}); }
</div>
)
}
}
export default PrescriptionIndex;
import React,{Component}来自'React';
从“react redux”导入{connect};
从“../actions/index.js”导入{fetchPrescriptionFromUrl};
从“反应路由器”导入{Link};
从“/new.jsx”导入新处方;
导入“../app.css”;
类PrescriptionIndex扩展了组件{
componentDidMount(){
this.props.fetchData(PORT+'/prescriptions');
}
render(){
if(this.props.has_错误){
返回(获取Api会导致错误)
}
如果(此.props.已加载){
返回(正在加载…)
}
返回(
{this.props.prescriptions.prescriptions&&this.props.prescriptions.prescriptions.map(函数(数据){
返回(
);
}); }
)
}
}
导出默认处方索引;
嗯,这是不可访问的代码。它前面的if
/else
语句已经从函数中返回了如果else
,并且在它们的体内返回,之后的任何代码都将不可访问。谢谢大家。问题解决了