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.
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.
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
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
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.
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!!!!'); }
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With