简体   繁体   English

对象作为 React 子级无效。 找到:带有键 {background, color} 的对象

[英]Objects are not valid as a React child. found: object with keys {background, color}

i am trying to setting up darkMode button, but i got this error ... Uncaught Error: Objects are not valid as a React child (found: object with keys {background, color}).我正在尝试设置 darkMode 按钮,但出现此错误...未捕获的错误:对象作为 React 子项无效(找到:带有键 {background,color} 的对象)。 If you meant to render a collection of children, use an array instead.如果您打算渲染一组子项,请改用数组。

here is my data..................这是我的数据......

my Contextjs component......我的 Contextjs 组件......

import { createContext, useReducer } from "react";
export const themeContext = createContext();


const initialState = {darkMode : true};

const themeReducer = (state, action) =>{

    switch(action.type){
        case'toggle':
        return {darkMode : !state.darkMode};
        default:
            return state;
    }
};

export const ThemeProvider = (props)=>{

    const [state, dispatch] = useReducer (themeReducer, initialState);
    return(
        <themeContext.Provider value={{state, dispatch}}>
           {props.children}
        </themeContext.Provider>
    );
};

my AppJs component.......我的 AppJs 组件.......

import Navbar from "./Components/Navbar/Navbar";
import "./App.css"
import { Intro } from "./Components/Intro/Intro";
import Services from "./Components/Services/Services";
import Experience from "./Components/Experience/Experience"
import Works from "./Components/Works/Works"
import Portfolio from "./Components/Portfolio/Portfolio"
import Testimonials from './Components/Testtimonials/Testimonials'
import Contact from "./Components/Contact/Contact"
import Footer from "./Components/Footer/Footer"
import {themeContext} from './Context'
import { useContext  } from "react";



function App() {

  const theme = useContext(themeContext);
  const darkMode = theme.state.darkMode;

  return (
    <div className="App">

      style={{ 
           background : darkMode? 'black' : '',
           color : darkMode? 'white' : ''
       }}
      
      <Navbar />
      <Intro />
      <Services />
      <Experience />
      <Works />
      <Portfolio/>
      <Testimonials/>
      <Contact />
      <Footer />
      
    </div>
  );
}

export default App;

and here is IndexJs..........这是IndexJs.......

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import {ThemeProvider} from './Context';

ReactDOM.render(
  <ThemeProvider> 
    <App />
  </ThemeProvider>,
  document.getElementById('root')
);


and error is:- Uncaught Error: Objects are not valid as a React child (found: object with keys {background, color}).并且错误是:-未捕获的错误:对象作为 React 子项无效(找到:带有键 {background,color} 的对象)。 If you meant to render a collection of children, use an array instead.如果您打算渲染一组子项,请改用数组。

You have to pass style prop to component.您必须将样式道具传递给组件。

<div
  className="App"
  style={{
    background: darkMode ? "black" : "",
    color: darkMode ? "white" : "",
  }}
>

I would rather prefer, create 2 classes dark and light and use like this:我宁愿创建 2 个深色和浅色类,并像这样使用:

<div className={`App  ${darkMode ? "dark" : "light"}`}>

It's trying to render your style object as a child of the div, update it so it is an attribute of the div.它试图将您的样式对象呈现为 div 的子对象,对其进行更新,使其成为 div 的属性。

<div
    className="App"
    style={{
        background: darkMode ? "black" : "",
        color: darkMode ? "white" : "",
    }}
>
    <Navbar />
    <Intro />
    <Services />
    <Experience />
    <Works />
    <Portfolio />
    <Testimonials />
    <Contact />
    <Footer />
</div>

暂无
暂无

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

相关问题 对象作为React子对象无效(找到:带有键{…}的对象) - Objects are not valid as a React child (found: object with keys {…}) 对象作为 React 子对象无效(找到:带有键的对象...) - Objects are not valid as a React child (found: object with keys…) 对象无效作为React子对象(找到:带有键的对象) - Objects are not valid as a React child (found: object with keys) 对象作为 React 子项无效(找到:object,键为 {}) - Objects are not valid as a React child (found: object with keys {}) React,错误:对象作为 React 子项无效(找到:object 和键 {data}) - React , Error: Objects are not valid as a React child (found: object with keys {data}) 错误:对象作为React子对象无效(找到:带有键的对象…) - Error: Objects are not valid as a React child (found: object with keys…) 如何修复“对象作为 React 子对象无效(找到:带有键 {} 的对象)” - How to fix "Objects are not valid as a React child (found: object with keys {}) 对象作为 React 子对象无效(找到:带有键 {author} 的对象) - Objects are not valid as a React child (found: object with keys {author}) 对象作为 React 子对象无效(找到:object 和键 {_id, name}) - Objects are not valid as a React child (found: object with keys {_id, name}) 对象作为 React 子对象无效(找到:object 和键 {username}) - Objects are not valid as a React child (found: object with keys {username})
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM