javascriptreactjspicturefill

Recommended way of using React + Picturefill


I want to use Picturefill + React + React Router (Also using Webpack).


Context: There is no isomorphic architecture yet so the data is fetched after initial page load (Route change).

…Because of this the render method is being called twice.

  1. Once for default state
  2. Once for updated state (fetched data)

Code

render: function () {
    return (
        <picture>
            <!-- You get the idea -->

            <source srcSet={this.props.large} media="(min-width: 1024px)" />
            <source srcSet={this.props.medium} media="(min-width: 640px)" />
            <img srcSet={this.props.small} />
        </picture>
    );
}

Example 1:

<head>
    <!-- Recommended usage -->
    <script src="https://cdn.rawgit.com/scottjehl/picturefill/2.3.1/dist/picturefill.js"></script>
</head>

Example 2:

// Use picturefill JavaScript API
componentDidUpdate() {
    picturefill();
}

More info/Alternatives?


Solution

  • I would recommend, that you use picturefill 3.0 RC1 in combination with the mutation plugin. This way you don't need to call picturefill();, everything is done automatically.

    This will work in any browser.