LoginSignup
4
4

More than 5 years have passed since last update.

rails + webpacker環境でredux-sagaを使ったら'regeneratorRuntime is not defined'が発生したときの解決方法

Posted at

概要

  • raisls + webpackerでredux-sagaを導入したら、regeneratorRuntime is not definedが発生
  • (redux-sagaのためにはじめて)定義したジェネレータ関数function*が原因
  • babel-polyfilで変換してやれば解決

環境

エラー

対象のコード

sagaが原因ではなく、ジェネレータ関数function*が原因なので本質的ではないですが、参考までにエラー対象のファイルを示します。

app/javascript/sagas/mySaga.js
import {call, takeLatest} from 'redux-saga/effects'
import {getColor} from '../modules/Api'
import {FETCH_COLOR} from '../actions/items'

export function* fetchColorFlow() {
  const response = yield call(getColor)
  if (response) {
    console.log(response)
  }
}

function* mySaga() {
  yield takeLatest(FETCH_COLOR, fetchColorFlow);
}

export default mySaga;

エラー内容

Uncaught ReferenceError: regeneratorRuntime is not defined

解決方法

babel-polyfilをインストールしてジェネレータ関数を使うファイルで読み込めばOK。

  • インストール
yarn add babel-polyfil
  • 読み込む
app/javascript/sagas/mySaga.js
import {call, put, takeLatest} from 'redux-saga/effects'
import {getColor} from '../modules/Api'
import {FETCH_COLOR} from '../actions/items'
import 'babel-polyfill' // これを読み込む
// 以下同じなので省略

参考

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4