2016-04-22 33 views
0

如果在浏览器中未检测到Flash,我试图隐藏元素。假设它看起来像这样。我在React中并使用JSX。如果在React JSX中未检测到闪存,我该如何隐藏元素

export default class MyComponent extends Component { 
    // Some code here 

    render() { 
    // some code here 

    return (
     <div> 
     <div> 
      <span>I am going to show no matter if flash is detected or not</span> 
     </div> 
     <div> 
      <span>I am not going to show if flash has not been detected</span> 
     </div> 
     </div> 
    ) 
    } 
} 

回答

1
export default class MyComponent extends Component { 
    constructor() { 
    super() 
    this.state = { flashSupported: false } 
    } 

    componentDidMount() { 
    //check flash is supported here 
    this.setState({ flashSupported: true }) 
    } 

    render() { 
    // some code here 
    const { flashSupported } = this.state 
    return (
     <div> 
      <div> 
      <span>I am going to show no matter if flash is detected or not</span> 
      </div> 
      { 
      flashSupported && (
      <div> 
       <span>I am not going to show if flash has not been detected</span> 
      </div>) 
      } 
     </div> 
    ) 
    } 
} 

更换({ flashSupported: true })真正闪光检测代码。

更新了4.25

如果脚本的结果为真,那么,闪光灯支持。(从https://gist.github.com/getify/675496粘贴)

((typeof navigator.plugins != "undefined" && typeof navigator.plugins["Shockwave Flash"] == "object") || (window.ActiveXObject && (new ActiveXObject("ShockwaveFlash.ShockwaveFlash")) != false)) 
+0

喂大卫,感谢。也许我应该在我的问题上更精确。我真的想知道如何知道是否支持Flash。看起来你在CDM中拥有它是有道理的。然而,什么是实际的代码或插件.. –

+0

嗨,答案更新。 –

+0

谢谢大卫! –