2017-06-21 2 views

答えて

0

を使用していますロードするための

長い時間がかかるときに読み込みを終了する必要があります。いずれかのキーが押されたとき、ちょうど脱出ボタンのキーコード、すなわち27

にしてそのキーコードを比較するイベントリスナーがcomponentDidMountに追加され、componentWillUnmountに除去する必要があり、反応します。

ここは例です。要件に応じてロジックを変更できます。

class App extends React.Component { 
    constructor(props) { 
    super(props) 
    this.state = { 
     loading: true, 
    } 
    this.onKeyUp = this.onKeyUp.bind(this) 
    } 
    componentDidMount() { 
    document.body.addEventListener('keyup', this.onKeyUp) 
    } 
    componentWillUnmount() { 
    document.body.removeEventListener('keyup', this.onKeyUp) 
    } 
    onKeyUp(e) { 
    if (/27/.test(e.keyCode)) { 
     this.setState({ loading: false }) 
    } 
    } 
    render() { 
    if (this.state.loading) { 
     return <div>Loading</div> 
    } 
    return <div>Loaded</div> 
    } 
} 

希望します。

+0

私はディスパッチに基づいて自動的にローダー反応ローダーをレンダリングしません。クエリ – sujeesh

+0

に記載されているローダーを表示します。次に、onKeyUpにアクションをディスパッチしてローダーを非表示にすることができます。 –

+0

どのように私にonKeyUpのロジックを送れますか – sujeesh

関連する問題