简体   繁体   English

反应:为什么我的 state 组件没有呈现?

[英]React: Why is my state component not rendering?

I am using context and reducer API for state management and logic.我正在使用上下文和减速器 API 进行 state 管理和逻辑。 The below code returns context provider, which is called in App.js.下面的代码返回上下文提供者,它在 App.js 中被调用。 Why isn't the component rendering?为什么组件不渲染?

SingerState.js 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;

The entire code is in here: https://codesandbox.io/s/heuristic-shockley-9u2xb?file=/src/App.js:0-411整个代码在这里: https://codesandbox.io/s/heuristic-shockley-9u2xb?file=/src/App.js:0-411

App.js应用程序.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>
  );
}

singerReducer.js歌手减速器.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;

singerContext.js歌手上下文.js

import { createContext } from "react";
const singerContext = createContext();
export default singerContext;

It should be {props.children} not {props.childer} in SingerState.js .在 SingerState.js 中应该是{props.children}而不是{props.childer} I made this correction inside your Sandbox and it is working.我在您的沙箱中进行了此更正,它正在工作。 To make sure, I changed initialState to this:为了确保,我将initialState更改为:

 const initialState = {
    name: "It's me",
    songs: null,
    loading: false,
  };

You have 2 typos in your code: 1- It should be {props.children} not {props.childer} in SingerState.js.您的代码中有 2 个拼写错误: 1- 在 SingerState.js 中应该是 {props.children} 而不是 {props.childer}。 (Thanks to yousoumar) (感谢优素玛)

And in SingerReducer.js.在 SingerReducer.js 中。 Instead of:代替:

switch (action.types) {
    case GET_SINGER:

use action.type because types is not defined使用action.type因为未定义types

Be carefully with typos!!!小心错别字!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM