Access props inside quotes in React JSX

后端 未结 9 1203
广开言路
广开言路 2020-12-04 06:11

In JSX, how do you reference a value from props from inside a quoted attribute value?

For example:



        
相关标签:
9条回答
  • 2020-12-04 06:48

    Here is the Best Option for Dynamic className or Props , just do some concatenation like we do in Javascript.

         className={
            "badge " +
            (this.props.value ? "badge-primary " : "badge-danger ") +
            " m-4"
          }
    
    0 讨论(0)
  • 2020-12-04 06:51

    you can use

    <img className="image" src=`images/${this.props.image}`>
    

    or

    <img className="image" src={'images/'+this.props.image}>
    

    or

      render() {
             let imageUrl = this.props.image ? "images/"+this.props.image : 'some placeholder url image';
        return (
           <div>
              <img className="image" src={imageUrl} />
           </div>
        )
      }
    
    0 讨论(0)
  • 2020-12-04 06:52

    For People, looking for answers w.r.t to 'map' function and dynamic data, here is a working example.

    <img src={"http://examole.com/randomview/images" + each_actor['logo']} />
    

    This gives the URL as "http://examole.com/randomview/images/2/dp_pics/182328.jpg" (random example)

    0 讨论(0)
提交回复
热议问题