Skip to content

Instantly share code, notes, and snippets.

@tetsuyainfra
Last active February 13, 2017 13:00
Show Gist options
  • Save tetsuyainfra/355e8a06e6e4a8a50e8fbf4bcff38e1f to your computer and use it in GitHub Desktop.
Save tetsuyainfra/355e8a06e6e4a8a50e8fbf4bcff38e1f to your computer and use it in GitHub Desktop.
turbolinks + react bootstrapはたぶんこんなかんじ
// ページ読み込みが完了した時に実行
document.addEventListener("DOMContentLoaded", e => {
console.log('fired ->', e.type, e.timeStamp)
// turbolinksを有効にしている時
document.addEventListener('turbolinks:load', (evt)=>{
console.log('will render ->', evt.type, evt.timeStamp)
let elm = document.getElementById('padapp')
if (!elm) { return ; }
ReactDOM.render(<Hello name="React" />, elm)
  // cacheとリクエスト結果の2回呼び出されることになるので、
  // 一回だけイベントを実行するようにオプションで設定する
  // 対応ブラウザがどうなってるか分からないので、onceオプション以外の実装も調べてみる?
  document.addEventListener('turbolinks:before-render', (evt)=>{
console.log('will unmountComponentAtNode ->', evt.type, evt.timeStamp)
ReactDOM.unmountComponentAtNode(elm)
}, {once: true})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment