Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
191
rated 0 times [  195] [ 4]  / answers: 1 / hits: 18662  / 6 Years ago, tue, january 30, 2018, 12:00:00

I am using ReactJs. I have two Components,PrescriptionIndex and PrescriptionNew, integrating one with another.



This is my first component 'PrescriptionNew'





import React, { Component } from 'react';
import FloatingActionButton from 'material-ui/FloatingActionButton';
import ContentAdd from 'material-ui/svg-icons/content/add';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider'

class PrescriptionNew extends Component {

render(){
return(
<div>
<MuiThemeProvider>
<FloatingActionButton mini={true} style={{color: pink}}>
<ContentAdd/>
</FloatingActionButton>
</MuiThemeProvider>
</div>
)
}
}

export default PrescriptionNew;





This is my another Component PrescriptionIndex





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>)
}
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;
}
return(
<div>
<PrescriptionNew /> //this is where I need my another component
</div>
)
}
}
export default PrescriptionIndex;





On Running, the PrescriptionNew component is not visible. On examing the console, warning is stated as Unreachable code. I have gone through other solutions, but I cannot able to study whats going wrong in my code.


More From » reactjs

 Answers
0

The reason is pretty straight forwards, you have an if-else and you are returning from both of them so the last part of your code is unreachable



you might want this



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;

[#55311] Saturday, January 27, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
andrewb

Total Points: 259
Total Questions: 124
Total Answers: 90

Location: Ivory Coast
Member since Sun, Mar 7, 2021
3 Years ago
;