3

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:

  • Alarm (return Alert Component)
  • Product Item (include function addToCart)

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})
6
  • 2
    I can't understand what you want, please create Minimal, Complete, and Verifiable example Commented Apr 25, 2019 at 17:51
  • this is unclear. Commented Apr 25, 2019 at 17:51
  • what errors are you encountering, it looks like you are returning a component from within a render function, which is expected. what is the issue? Commented Apr 25, 2019 at 17:52
  • It's not very clear what you mean, but I think you want to show an alert when a user successfully added an item to the cart? If so, you could use the local state of the component you are developing, which could contain a value that makes the render method know whether to render the alert or not. More info: reactjs.org/docs/… Commented Apr 25, 2019 at 17:58
  • @Vencovsky, Sorry for unclear question, I edited it. Commented Apr 25, 2019 at 18:05

2 Answers 2

2

As I understand you want to render Alarm(which contains Alert) component. There are two places where you can do it.

  1. Inside Product Item component or inside any component: For this one, you can use some local state or maybe a redux state
        render(){
          // const {showAlert} = this.props; // if redux state
          const {showAlert} = this.state; // local state
          return(
              <div>
                { showAlert &&
                  <Alarm />
                }      
              </div>
            )
          }
  1. Outside the DOM hierarchy of the parent component: Then you can use the Portals. Because, Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component. This is mostly useful for loaders and alert message pop-ups.
Sign up to request clarification or add additional context in comments.

Comments

2

'Standard comunication' between components in react is done by passing props from parent to child (even a few levels deep). Read docs. Common/app wide state can be deployed using redux, context API, apollo etc.

In this case you probably need to use portals.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.