Search code examples
javascriptreactjsreact-routermern

Redirecting to external link using ReactJS


I want to add button property which will redirect to external link when it is clicked. prop which should be added to button is siteURL .Button should be in class = "project-item-details-container". Do I have to install any external package?

sample button: Visit site

code which I have written -

  const ProjectItem = props => {
  const {projectDetails} = props
  const {projectId, imageURL, description, title, siteURL} = projectDetails
  return (
    <>
      <li className="project-item-container">
        <img
          className="project-item-image"
          src={imageURL}
          alt={`project-item${projectId}`}
        />
        <div className="project-item-details-container">
          <h1 className="project-item-title">{title}</h1>
          <p className="project-item-description">{description}</p>
        </div>
      </li>
    </>
  )
}

Solution

  • Well, I solved this. I have used attribute in button element.

    const ProjectItem = props => {
      const {projectDetails} = props
      const {projectId, imageURL, description, title, siteURL} = projectDetails
      return (
        <>
          <li className="project-item-container">
            <img
              className="project-item-image"
              src={imageURL}
              alt={`project-item${projectId}`}
            />
            <div className="project-item-details-container">
              <h1 className="project-item-title">{title}</h1>
              <p className="project-item-description">{description}</p>
              <button type="button" className="project-redirect-button">
                <a href={siteURL} alt="Broken Link">
                  Visit Site
                </a>
              </button>
            </div>
          </li>
        </>
      )
    }