Skip to content
⚠️ This article was written in 2019. Some content may be outdated.

React Error Boundary:ErrorBoundary

多くの方が React のエラーバウンダリ(ErrorBoundary)について誤った理解をされています。本記事では、重要なポイントとよくある誤解を体系的に整理します。

クイックスタート ​

次の方法で実装できます:

javascript
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].[contenthash:8].js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: 'babel-loader'
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader']
      }
    ]
  },
  optimization: {
    splitChunks: {
      chunks: 'all',
      cacheGroups: {
        vendor: {
          test: /[\\/]node_modules[\\/]/,
          name: 'vendors'
        }
      }
    }
  },
  plugins: [
    new HtmlWebpackPlugin({ template: './src/index.html' }),
    new MiniCssExtractPlugin({ filename: '[name].css' })
  ]
}

上記のコード内のパフォーマンス上の細部に注意し、不要な計算を避けてください。

高度な使い方 ​

具体的な実装は次のコードを参考にしてください:

javascript
const { sum, debounce } = require('./utils')

describe('utils', () => {
  test('sum 计算正确', () => {
    expect(sum(1, 2)).toBe(3)
    expect(sum(-1, 1)).toBe(0)
  })

  test('debounce 延迟执行', () => {
    jest.useFakeTimers()
    const fn = jest.fn()
    const debounced = debounce(fn, 300)

    debounced()
    debounced()
    debounced()
    expect(fn).not.toHaveBeenCalled()

    jest.advanceTimersByTime(300)
    expect(fn).toHaveBeenCalledTimes(1)
  })
})

本番環境で検証済みで、このアプローチは安定して動作します。

ビジネス上の適用場面 ​

基本的な使い方から見てみましょう:

javascript
{% raw %}
<template>
  <div>
    <p>{{ message }}</p>
    <button @click="reverse">反转</button>
  </div>
</template>

<script>
export default {
  data() {
    return { message: 'Hello Vue 2' }
  },
  methods: {
    reverse() {
      this.message = this.message.split('').reverse().join('')
    }
  }
}
</script>
{% endraw %}

この書き方はシンプルで分かりやすく、ほとんどの場面に適しています。

落とし穴回避ガイド ​

中心的な実装コードは次のとおりです:

javascript
export default {
  props: ['items'],
  computed: {
    sorted() {
      return [...this.items].sort((a, b) => b.score - a.score)
    },
    count() {
      return this.items.length
    }
  },
  filters: {
    formatDate(val) {
      return new Date(val).toLocaleDateString('zh-CN')
    }
  }
}

実際のプロジェクトでは、境界条件(エッジケース)と例外処理も考慮する必要があります。

クイックスタート ​

以下は実際の例です:

javascript
export default {
  directives: {
    focus: {
      inserted(el) {
        el.focus()
      }
    },
    loading: {
      bind(el, binding) {
        if (binding.value) {
          el.classList.add('loading')
        }
      },
      update(el, binding) {
        el.classList.toggle('loading', binding.value)
      }
    }
  }
}

このパターンをチーム内に広めたところ効果は良好で、保守コストが明らかに低下しました。

まとめ ​

  • 継続的に学び、振り返りを行い、技術に対する感度を保ちましょう
  • 問題に直面したら、ソースコードと公式ドキュメントをよく確認しましょう
  • React のエラーバウンダリ(ErrorBoundary)の鍵は、中心的な考え方を理解することにあり、表面的な使い方にとどまらないことです

MIT Licensed