render() {
<Img src={path} onError={handleError} />
}
###Props
####src
PropTypes.string.isRequired
This is path of the image that has to be rendered.
####onError
PropTypes.func
Error Handler in case image is not found at specified path or on network
To see the component in action please follow below steps.
- Clone this repo
- Do npm install & npm start
- Bydefault content will be served at localhost:80, access it using browser.
Steps to test.
- We can modify any image path in https://github.com/gautam123456/image-component/tree/master/src/components/Main.js so that we are sure that path is incorrect.
- By default even if image is not found a default image will be served through error handler method.
- We can comment out call to props.onError method call and default image will not be server in case of error.
Project Structure:
- There are components at directory https://github.com/gautam123456/image-component/tree/master/src/components
- Required Image Component is at path https://github.com/gautam123456/image-component/tree/master/src/components/Img.js
- Data is served through https://github.com/gautam123456/image-component/tree/master/src/components/Main.js
- Webpack entry point is at https://github.com/gautam123456/image-component/tree/master/src/index.js