React 应用程序组件未按预期工作

问题描述 投票:0回答:1

这是主要的 React 应用程序。在导航栏组件中,我尝试通过道具更改主反应应用程序的背景颜色。但它并没有改变颜色。

import './App.css';
import About from './components/About';
import Navbar from './components/Navbar';
import Textform from './components/Textform';
import React, {useState} from 'react'



function App() {
  const [mod, setmod] = useState("lightl")

  return (
    <>
       <Navbar title="Hello" mode={mod} />
       <div className ="container">
        <Textform/>
       </div>
      <div className ="container">
        <About/>
      </div>
    </>
     
  );
}

export default App;
  

这是导航栏组件。

import React from 'react'

export default function Navbar(props) {
  return (
    <div>
      <nav className={`navbar navbar-expand-lg navbar-${props.mod} bg-${props.mod}`}>
    <div className="container-fluid">
      <a className="navbar-brand" href="/">{props.title}</a>
      <button className="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span className="navbar-toggler-icon"></span>
      </button>
      <div className="collapse navbar-collapse" id="navbarSupportedContent">
        <ul className="navbar-nav me-auto mb-2 mb-lg-0">
          <li className="nav-item">
            <a className="nav-link active" aria-current="page" href="/">Home</a>
          </li>
          <li className="nav-item">
            <a className="nav-link" href="/">Link</a>
          </li>
          <li className="nav-item dropdown">
            <a className="nav-link dropdown-toggle" href="/" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Dropdown
            </a>
            <ul className="dropdown-menu" aria-labelledby="navbarDropdown">
              <li><a className="dropdown-item" href="/">Action</a></li>
              <li><a className="dropdown-item" href="/">Another action</a></li>
              <li><hr className="dropdown-divider"/></li>
              <li><a className="dropdown-item" href="/">Something else here</a></li>
            </ul>
          </li>
          <li className="nav-item">
            <a className="nav-link disabled" href="/" tabIndex="-1" aria-disabled="true">Disabled</a>
          </li>
        </ul>
        <form className="d-flex">
          <input className="form-control me-2" type="search" placeholder="Search" aria-label="Search"/>
          <button className="btn btn-outline-success" type="submit">Search</button>
        </form>
      </div>
    </div>
  </nav>
    </div>
  )
}

我在终端中发现以下警告

https://i.sstatic.net/FrUE6FVo.png) (https://i.sstatic.net/IYIhMolW.png)

浏览器检查中出现此错误

https://i.sstatic.net/9nYo4YXK.png

我错过了什么以及我应该改变什么。我还使用引导程序来设计风格。该课程来自《Coding with harry》。

reactjs components bootstrap-5 manifest
1个回答
0
投票

你需要从 mod 状态传递整个类 例如

// ther right way
const [mod, setMod] = useState('navbar-lightl')
© www.soinside.com 2019 - 2024. All rights reserved.