onError the preloader should be used
vinnymac opened this issue · comments
The default behavior should display the preloaded image if the specified image fails to load. Currently nothing is rendered if an image fails to load. We have onError, which is great. Something like postLoader or errorLoader so we can display something when an error occurs.
This can be done easily enough:
React.createClass
getInitialState: -> hasError: false
render: ->
preloader = -> <img src = "cat.gif"/>
if @state.hasError
preloader()
else
<ImageLoader
src = { @props.src }
preloader = { preloader }
onError = { => @setState hasError: true }
/>
Thoughts?
If you give ImageLoader
any children, they will be rendered if the image fails to load:
React.createClass
render: ->
preloader = -> <img src = "cat.gif"/>
<ImageLoader
src = { @props.src }
preloader = { preloader }
>
{ preloader() }
</ImageLoader>
Will that work for you?
@lettertwo yea that is much better than what I had proposed, I did not know you could do that. Thank you much! I saw the text in the example, but never put two and two together. Perhaps adding the above example or one similar, to the ReadMe
would help in the future?