How to chose a React component props depending on condition

react conditional render multiple elements
react: render conditionally from props
conditional rendering react
conditional rendering react native
conditional rendering react hooks
react render component onclick
react conditional rendering best practices
react: use a ternary expression for conditional rendering

I have the following React component:

<SweetAlert 
    show={this.props.message} 
    success 
    title={this.props.message}
    onConfirm={this.props.handleCloseAlert}>
</SweetAlert>

And this is the alert I get with it:

But I want the props success to be dynamically chosen at execution time, so I tried this:

 const alertType = () => {
    switch(this.props.type) {
        case ALERT_ERROR:
          return 'error'
        case ALERT_WARNING:
          return 'warning'
        case ALERT_DANGER:
          return 'danger'
        case ALERT_INFO:
          return 'info'
        case ALERT_SUCCESS:
          return 'success'
      }
    }

<SweetAlert 
    show={this.props.message} 
    {...alertType()}
    title={this.props.message}
    onConfirm={this.props.handleCloseAlert}>
</SweetAlert>

But I loose the alert type with it:

I haven't found a way for adding a discretional props to a component.


You should pass type as the returned value of your function

<SweetAlert 
    show={this.props.message} 
    type={alertType()}
    title={this.props.message}
    onConfirm={this.props.handleCloseAlert}>
</SweetAlert>

Source of why passing success as a boolean worked

8 conditional rendering methods in React, There's more than one way to use conditional expressions in React. And, as Component { constructor(props) { super(props); this.state = {text: '', inputText: '' You can decide which one is best for your situation based on:. class App extends React.Component {constructor(props) which has the ternary expression inside it to show ON or OFF, depending on the this It works because if the condition this.state


if u return a object for the component, like {danger: true}

switch(this.props.type) {
        case ALERT_ERROR:
          return {error: true}
        case ALERT_WARNING:
          return {warning: true}
        case ALERT_DANGER:
          return {danger: true}
        case ALERT_INFO:
          return {info: true}
        case ALERT_SUCCESS:
          return {success: true}
      }
    }

Three ways to conditionally render components with React and JSX , Which one you should choose depends on the situation. Let's assume that this parent component has a prop named isAdmin that we'll use to� Basically that's how props are passed to React components. As you may have noticed, props are only passed from top to bottom in React's component tree. There is no way to pass props up to a parent component. We will revisit this issue later in this article. In addition, it's important to know that React's props are read only.


Writing

<MyComponent myProp/>

is just a short hand notation for

<MyComponent myProp={true}/>

So changing return 'error' to return {error: true} should work for you

7 Ways to Implement Conditional Rendering in React Applications , As a challenge, based on the value of isLoggedIn in our component state, we want AuthButton.js import React from "react"; const AuthButton = props => { let Thus we can conditionally assign elements/ components to these variables and the choice of which to use is mostly dependent on the use case. You can learn more here about how React updates the DOM elements and how the diffing algorithm works.. Maybe in this simple example, the performance improvement is insignificant, but when working when big components, there can be a difference.


4 React conditional rendering methods with props and state, 4 React conditional rendering methods with props and state But what if you want to render a specific HTML element or React component depending on a prop or In the render method I'm adding a condition to check if I need to yell at the But it's up to you to decide what is a readable, and appropriate style to use for you� Use JavaScript operators like if or the conditional operator to create elements representing the current state, and let React update the UI to match them. Consider these two components: function UserGreeting(props) { return <h1>Welcome back!</h1>; } function GuestGreeting(props) { return <h1>Please sign up.</h1>; }


Conditionally adding props to React Components, In React, you can conditionally add attributes to React components. You can do this by using inline conditionals inside the attribute, if statements, or even a� At some point, when building a React component, you'll run into a situation where you need to apply a class based on some condition. Perhaps, you need to make an image bigger based on some state, or you need to make the image round instead of square based on a prop, or you want to truncate some text based on a user’s action.


React Conditional Rendering, In a conditional render, a React component decides based on one or The condition is a JavaScript boolean which comes in as React prop. You can use the boolean to decide which element you want to conditionally render:. How To Share State Across React Components with Context. In this tutorial, you'll share state across multiple components using React context. React context is an interface for sharing information with other components without explicitly passing the data as props.