Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

click an image for outside url react.js

I am working on a portfolio and I'm using react.js. I simply want to click an image, for example, a StackOverflow icon, and be able to redirect to the page. I'm seeing all sorts of different ways to go about, yet I cannot get it to redirect.

Click to see the image of icons

I am using React-Bootstrap which I don't know if that is going to change anything.

export default class Home extends Component {
render() {
    return (
        <Grid>
            <Jumbotron className="brainImg">
            </Jumbotron>
            <div class="footer"><center className="iconsBaby">
                <Image src="giticon.png" className="githubIcon" to="https://github.com/Joeyryanbridges" />
                <Image src="linkedinIcon.png" className="linkedinIcon" href="https://github.com/Joeyryanbridges" />
                <Image src="SOFIcon.png" className="githubIcon" href="https://github.com/Joeyryanbridges" />
            </center>
            </div>
        </Grid>
    )
}

Thank you for looking.

like image 724
joeyryanbridges Avatar asked Jul 26 '18 07:07

joeyryanbridges


People also ask

How to load images from URL in ReactJS?

In App.js, multiple images are passed as a property to a component. this way, It is very easy and simple to load images from URL in reactjs. In React applications, Images are served from different folder locations. if images are in src folder, you have to import the image with a path from using import feature

How to display an image in react app?

if you want to display the image in react, Create a component as follows. It is simple to call the component in javascript or JSX file For example, In App.js Here, The image url is loaded in the component and displayed to the browser. There are a couple of improvements to this component

How to load an image from a URL in a component?

In The component, Move the setting of the URL to the outside component. Use this.props to load an image from the URL. Set src using this.props.url value. Props are directly accessible in a component, even though declaring a constructor. It works without a constructor. In App.js, multiple images are passed as a property to a component.

How to use imageclick() function in react functional component?

Functional component in React are stateless (instance less). So, they don't have access to this keyword. Therefore, the second and third example won't work. Simply, use the first example function imageClick () { console.log ('Click!!!!'); }


2 Answers

Generally an Image component should not be a link on its own. What you should do is wrap your image component with an <a> tag or use the Link component if you're using react-router.

<a href="https://github.com/Joeyryanbridges">
  <Image src="giticon.png" className="githubIcon" />
</a>

OR with react-router Link

<Link to="https://github.com/Joeyryanbridges">
  <Image src="giticon.png" className="githubIcon" />
</Link>

This way the Image component is not concerned with redirects or changing URLs and that functionality is handled by the proper component or tag.

Always keep in mind that separation of concerns is very important when it comes to reusability and maintainability.

like image 120
Alizoh Avatar answered Sep 21 '22 13:09

Alizoh


Just wrap tag inside an like this:

   <a href="abc.com">
    <Image src="abc.png" />
   </a>

Or If you are using react-router,then you can do this:

   import { Link } from "react-router-dom";

   <Link to="www.abc.com">
   <Image src="abc.png" />
   </Link>

Hope this help:

like image 34
Adnan Avatar answered Sep 21 '22 13:09

Adnan