I want to load a new component on the button click in react. A new component should completely replace a current component - I mean that it should be loaded on a whole screen (not anywhere next to buttons).
import React from "react";
class MasterIncident extends React.Component {
render() {
return (
<React.Fragment>
<button
variant="contained"
size="medium"
color="primary"
value="single"
name="1"
onClick={this.props.clickBtn}
>
Add
</button>
<button
variant="contained"
size="medium"
color="primary"
value="batch"
name="2"
onClick={this.props.clickBtn}
>
Export
</button>
<h3>Master Incident Inventory</h3>
</React.Fragment>
);
}
}
export default MasterIncident;
import MasterIncident from './MasterIncident';
import MasterAddPage from './MasterAddPage';
class MasterCreate extends Component {
state = {
renderView: 0
};
clickBtn = e => {
this.setState({
renderView: +e.target.parentNode.name
});
};
render() {
switch (this.state.renderView) {
case 1:
return <MasterAddPage />;
default:
return <MasterIncident clickBtn={this.clickBtn} />;
}
}
}
export default MasterCreate
const MasterAddPage = (props) => {
console.log(props)
return (
<div >Content</div>
)
}
export default MasterAddPage
It gives an error TypeError: Class constructor MasterIncident cannot be invoked without new
.