简体   繁体   English

object object output 的使用状态钩子

[英]object object output of useState hook

Greetings iam in need of help so I am taking an online course of react and I followed sir Kalob step by step but while assigning keyword using usestate() it is printing [object object] instead of the actual keyword which I set using hook and so I tried to look for different solutions from useState("searching for") I changed it to useState({name:"searching for"}) and while assigning it to keyword i changed it from keyword={searchText} to keyword={searchText.name} but still, it's giving me the same output I am out of solutions any help would be appreciated.问候,我需要帮助,所以我正在参加一个在线反应课程,我一步一步跟着卡洛布先生,但是在使用 usestate() 分配关键字时,它正在打印 [object object] 而不是我使用 hook 设置的实际关键字等等我试图从 useState("searching for") 中寻找不同的解决方案,我将其更改为 useState({name:"searching for"}) 并在将其分配给关键字时将其从关键字 = {searchText} 更改为关键字 = {searchText .name} 但它仍然给了我同样的 output 我没有解决方案任何帮助将不胜感激。 All necessary screenshots are attached.附上所有必要的屏幕截图。 enter image description here在此处输入图像描述

enter image description here在此处输入图像描述

enter image description here在此处输入图像描述

APP.JS APP.JS

import Navbar from './components/navbar';
import Home from './components/home';
import React from 'react';
import SearchView from './components/SearchView'
import {useState,useEffect} from 'react';
import AboutView from './components/AboutView';
import {Routes , Route} from 'react-router-dom';
import './App.css';

function App() {
 const[searchResults,setSearchResults] = useState([]);
 const [searchText,setSearchText] = useState('');
 
  return (
    <div>
    <Navbar searchText={searchText} setSearchText={setSearchText}/>
    <Routes>
    <Route path="/" element={<Home />} />
    <Route path='/about' element={<AboutView />} />
    <Route path='/search' keyword= {searchText}searchResults={searchResults} element ={<SearchView />} />
    </Routes>
    </div>
  );
}

export default App;

Navbar.Js导航栏.Js

// import App from "../App"
import React from 'react';
import {Link} from 'react-router-dom';

const Navbar = ({searchText,setSearchText}) => {
  const updateSearchText = (e) =>{
    setSearchText(e.target.value)
  }
    return(
      <div>
        <nav className="navbar navbar-expand-lg bg-light">
    <div className="container-fluid">
      <button className="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
        <span className="navbar-toggler-icon"></span>
      </button>
      <Link className="navbar-brand" to="/">Movie Browser </Link>
      <div className="collapse navbar-collapse" id="navbarTogglerDemo03">
        <ul className="navbar-nav me-auto mb-2 mb-lg-0">
          <li className="nav-item">
            <Link className="nav-link active" aria-current="page" to="/">Home</Link>
          </li>
          <li className="nav-item">
            <Link className="nav-link" to="/about">About</Link>
          </li>
          <li className="nav-item">
            <a className="nav-link disabled" href="/">Disabled</a>
          </li>
        </ul>
        <form className="d-flex" role="search">
          <input className="form-control me-2" type="search" placeholder="Search" aria-label="Search" value={searchText}
          onChange={updateSearchText}/>
          <button className="btn btn-outline-success" type="submit">Search</button>
        </form>
      </div>
    </div>
  </nav>
      </div>
    )
  }
  export default Navbar;

SearchView.js搜索视图.js

import React from "react";
import Hero from "./Hero";

const SearchView =(keyword,searchResults)=>{
    const title =  `You are searching for ${keyword}`
    console.log(searchResults,"are the search results")
    return(
        <div>
            <Hero text ={title} />
        </div>
    )
}
export default SearchView;
 

Pass the props that SearchView should have through the component prop while calling the component.在调用组件时,通过component prop 传递SearchView应该具有的 props。

<Route 
  path='/search' 
  component={
    <SearchView keyword={searchText} searchResults={searchResults} />
  } 
/>

And your output is [object Object] because you're logging the props object, not the keyword string.而您的 output 是[object Object]因为您正在记录props object,而不是keyword字符串。 Add the destructuring assignment of the props to actually get the props that are being passed.添加 props 的解构赋值以实际获取正在传递的 props。

const SearchView = ({ keyword, searchResults }) => {
  const title = `You are searching for ${keyword}`;

  return (
    <div>
      <Hero text ={title} />
    </div>
  );
}

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

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