简体   繁体   中英

how to render multiple copies of this component in react jsx

I want to create another copy of this component function and each time a new copy is created i want to change the <h3> and <p> tag

function Ads(product) {
    return(
      <div className = "row" id="user-ads">
        <div className = "col-sm-6 col-md-5">
          <div className = "thumbnail">
            <img src = "img/img1.jpg" alt = "Generic placeholder thumbnail" />
          </div>
          <div className = "caption">
            <div className="border">
              <h3>{product.title}</h3>
              <p>{product.desc}</p>  
              <button className = "btn btn-primary" role = "button" data-toggle="modal" data-target="#view-detail">View Details
              </button>  
            </div>
          </div>
        </div>
      </div>
    );
}
ReactDOM.render(<Ads title="PlayStation 4" desc="Lorem ipsum jipsum Lorem ipsum jipsum"/>, document.getElementById('ads'));

Something like this should work:

var configObjects = [
  {
    desc: "Lorem ipsum jipsum Lorem ipsum jipsum",
    img: "img1.jpg",
    title: "PlayStation 4"
  },
  {
    desc: "Lorem ipsum jipsum Lorem ipsum jipsum",
    img: "img2.jpg",
    title: "PlayStation 5"
  },
  {
    desc: "Lorem ipsum jipsum Lorem ipsum jipsum",
    img: "img3.jpg",
    title: "PlayStation 6"
  }
]

function Ads() {
  var multipleAds = AdsAssembly(configObjects);
  return(
    <div>{multipleAds}</div>  
  );
}

function AdsAssembly(array) {
  var jsxArray = [];
  array.forEach(function (adObject, index) {
    jsxArray.push(AdsContent(adObject));
  })
  return jsxArray;
}

function AdsContent(object) {
  return (
    <div className = "row" id="user-ads">
      <div className = "col-sm-6 col-md-5">
        <div className = "thumbnail">
          <img src ={"img/" + object.img} alt = "Generic placeholder thumbnail" />
        </div>
        <div className = "caption">
          <div className="border">
            <h3>{object.title}</h3>
            <p>{object.desc}</p>  
            <button className = "btn btn-primary" role = "button" data-toggle="modal" data-target="#view-detail">View Details
            </button>  
          </div>
        </div>
      </div>
    </div>
  )
}
ReactDOM.render(<Ads/>, document.getElementById('ads'));

You can use the configuration array to dynamically populate each ad you would like to display. Cheers!

Alternatively you could pass the configs as a property like so:

ReactDOM.render(<Ads configs={configObjects} />, document.getElementById('ads'));

Then use them like this:

function Ads(props) {
      var multipleAds = AdsAssembly(props.configs);
      return(
        <div>{multipleAds}</div>  
      );
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM