简体   繁体   中英

How do i choose import/export in React

I want to use WebP when the user open the Page in Chrome and else it should be png.

i found this Code:

var isChrome = !!window.chrome && !!window.chrome.webstore

But i cant put an If-statement around export or import

This is my Code so far:

import redditwebp from '../img/icons/reddit.webp';
import redditpng from '../img/icons/reddit.png';

var isChrome = !!window.chrome && !!window.chrome.webstore;

if(isChrome){
  export default {redditwebp}
}
else{
  export default {redditpng}
}

you shouldnt really be doing normal if statements you should do an inline one

const isChrome = !!window.chrome && !!window.chrome.webstore;

then in your actual code inline

<div>{isChome ? <img src={redditwebp} alt="" /> : <img src={redditpng} alt="" />}</div>

to me that is the best way to do it, you may have to write this.isChrome im not sure whether you will or not.

i will make this clearer.

import redditwebp from '../img/icons/reddit.webp';
import redditpng from '../img/icons/reddit.png';
import React, { Component } from 'react';

const isChrome = !!window.chrome && !!window.chrome.webstore;

export default class logo extends Component {
render(){
    return (
        <div>{isChome ? <img src={redditwebp} alt="" /> : <img src={redditpng} alt="" />}</div>
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM