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

281
Views
Reaccionar: ¿Por qué mi componente de estado no se procesa?

Estoy usando contexto y API reductora para la lógica y la gestión del estado. El siguiente código devuelve el proveedor de contexto, que se llama en App.js. ¿Por qué no se renderiza el componente?

SingerState.js

 import React, { useReducer } from "react"; import SingerContext from "./singerContext"; import SingerReducer from "./singerReducer"; import { GET_SINGER, SET_LOADING } from "../types"; const SingerState = (props) => { console.log('foo') const initialState = { name: null, songs: null, loading: false, }; const [state, dispatch] = useReducer(SingerReducer, initialState); const getSinger = () => { dispatch({ type: GET_SINGER, payload: { name: "jb", songs: ["song1", "song2"], }, }); }; const setLoading = () => { dispatch({ type: SET_LOADING, }); }; return ( <SingerContext.Provider value={{ name: state.name, songs: state.songs, loading: state.loading, getSinger, setLoading, }} > {props.childer} </SingerContext.Provider> ); }; export default SingerState;

El código completo está aquí: https://codesandbox.io/s/heuristic-shockley-9u2xb?file=/src/App.js:0-411

Aplicación.js

 import "./App.css"; // import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; import SingerState from "./context/singer/singerState"; import Navbar from "./components/Navbar"; import Singer from "./components/singer/Singer"; export default function App() { return ( <div className='App'> <Navbar /> <SingerState> <Singer /> </SingerState> </div> ); }

cantanteReductor.js

 import { GET_SINGER, SET_LOADING } from "../types"; const reducer = (state, action) => { switch (action.types) { case GET_SINGER: return { ...state, name: action.payload.name, song: action.payload.song, loading: false, }; case SET_LOADING: return { ...state, loading: true, }; default: return state; } }; export default reducer;

cantanteContexto.js

 import { createContext } from "react"; const singerContext = createContext(); export default singerContext;
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Debería ser {props.children} no {props.childer} en SingerState.js . Hice esta corrección dentro de su Sandbox y está funcionando. Para asegurarme, cambié initialState a esto:

 const initialState = { name: "It's me", songs: null, loading: false, };
about 4 years ago · Juan Pablo Isaza Report

0

Tiene 2 errores tipográficos en su código: 1- Debería ser {props.children} no {props.childer} en SingerState.js. (Gracias a yousomar)

Y en SingerReducer.js. En vez de :

 switch (action.types) { case GET_SINGER:

use action.type porque los types no están definidos

¡¡¡Cuidado con los errores tipográficos!!!

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!