Reputation: 2132
So i need to render "Alert" component, which informs user about succesfully action in component - inside function, which adding item to cart/localstorage.
Alarm Component:
class Alarm extends React.Component{
render(){
return(
<Alert color="success">
This is a success alert — check it out!
</Alert>
)
}
}
Function in another component:
addToCart = () => {
(Some Logic...)
}
EDIT
I have 2 components:
After initializing function addToCard I need to render Alarm Component
SOLUTION
I declare "x" in constructor state:
constructor(props) {
super(props);
this.state = {
quantity: 1,
x: false
}
}
And add to ProductItem component:
<div>
{ this.state.x &&
<Alarm />
}
</div>
And inside function just:
this.setState({x: true})
Upvotes: 3
Views: 10839
Reputation: 222
As I understand you want to render Alarm(which contains Alert) component. There are two places where you can do it.
render(){
// const {showAlert} = this.props; // if redux state
const {showAlert} = this.state; // local state
return(
<div>
{ showAlert &&
<Alarm />
}
</div>
)
}
Upvotes: 2