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

React Context Usage Guide

React Context is something I reach for often in daily work, so I've put together a systematic write-up, hoping it helps others understand and apply it better.

Basic Concepts ​

Here's one way to implement it:

javascript
// 使用示例
import { createApp } from './app'

const config = {
  apiBase: process.env.API_BASE || '/api',
  timeout: 10000,
  retries: 3
}

const app = createApp(config)
app.mount('#root')

This pattern improves the maintainability of the code.

Core Implementation ​

The specific usage is shown in the code below:

javascript
// 工具函数封装
function createHandler(options = {}) {
  const { timeout = 5000, retries = 3 } = options

  return async function execute(url, data) {
    for (let attempt = 0; attempt < retries; attempt++) {
      try {
        const controller = new AbortController()
        const timer = setTimeout(() => controller.abort(), timeout)
        const res = await fetch(url, {
          method: 'POST',
          body: JSON.stringify(data),
          signal: controller.signal
        })
        clearTimeout(timer)
        return await res.json()
      } catch (err) {
        if (attempt === retries - 1) throw err
      }
    }
  }
}

It's worth standardizing this across the team to reduce inconsistencies.

Practical Application ​

Let's look at a concrete implementation:

javascript
// 核心实现
const processData = (input) => {
  return input
    .filter(item => item.active)
    .map(item => ({
      ...item,
      displayName: item.name.trim(),
      timestamp: Date.now()
    }))
    .sort((a, b) => b.timestamp - a.timestamp)
}

This approach is clean and efficient, and fits most scenarios.

Best Practices ​

Here is a real-world example:

javascript
// 使用示例
import { createApp } from './app'

const config = {
  apiBase: process.env.API_BASE || '/api',
  timeout: 10000,
  retries: 3
}

const app = createApp(config)
app.mount('#root')

In real projects you'll still need to adapt it to your specific requirements.

Common Issues ​

The core code is as follows:

javascript
// 工具函数封装
function createHandler(options = {}) {
  const { timeout = 5000, retries = 3 } = options

  return async function execute(url, data) {
    for (let attempt = 0; attempt < retries; attempt++) {
      try {
        const controller = new AbortController()
        const timer = setTimeout(() => controller.abort(), timeout)
        const res = await fetch(url, {
          method: 'POST',
          body: JSON.stringify(data),
          signal: controller.signal
        })
        clearTimeout(timer)
        return await res.json()
      } catch (err) {
        if (attempt === retries - 1) throw err
      }
    }
  }
}

Make sure to handle edge cases and exceptions properly.

Summary ​

  • Keep up with community developments and update your technical approach in time.
  • Performance optimization should be based on real data; avoid over-optimizing.
  • The core of React Context is understanding the underlying principles, not just memorizing the API.
  • In real projects, picking the right solution matters more than chasing the newest tech.

MIT Licensed