Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
por qué estoy recibiendo NaN

Intenté este patrón de incluir la funcionalidad de despacho en mi función reductora, pero obtuve este NaN, independientemente de lo que intente aumentar o disminuir, ¿qué es lo que me falta? Básicamente, crear un método auxiliar en mi función reductora para un mejor enfoque de API podría no ser el mejor enfoque, pero al menos probé las cosas de manera diferente, ¿cuál es el problema que me falta para que esto funcione? [perdone mi inglés]

 import * as React from 'react'; const CounterContext = React.createContext(); function CounterProvider({ step = 1, initialCount = 0, ...props }) { const [state, dispatch] = React.useReducer( (state, action) => { const change = action.step ?? step switch (action.type) { case 'increment': { return { ...state, count: state.count + change } } case 'decrement': { return { ...state, count: state.count - change } } default: { throw new Error(`Unhandled action type:${action.type}`) } } }, { initialCount: initialCount }) const increment = React.useCallback(() => dispatch({ type: 'increment' }), [ dispatch ]) const decrement = React.useCallback(() => dispatch({ type: 'decrement' }), [ dispatch ]) const value = { increment, decrement, state } return <CounterContext.Provider value={value} {...props} /> } function useCounter() { const context = React.useContext(CounterContext) if (context === undefined) { throw new Error('useCounter must be used within CounterProvider') } return context } function Counter() { const { state, increment, decrement } = useCounter(); console.log(state, 'inc ', increment, 'dec ', decrement) return ( <div> <button onClick={decrement}>-</button> <button onClick={increment}>+</button> <div>Current Count: {state.count}</div> </div> ) } function App() { return ( <CounterProvider> <Counter /> </CounterProvider> ) } export default App
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Porque pasas el valor initial es { initialCount: initialCount } . Así que no tienes count . Solo actualiza para count

 { count: initialCount }
about 4 years ago · Juan Pablo Isaza Report

0

Es simple, solo tiene que actualizar su { initialCount: initialCount } para contar para obtener el valor.

Debería verse así { cuenta: cuenta inicial }

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!