2016-04-22 60 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

謝謝大衛! –