List.jsx:12 未捕获的类型错误:无法读取 null 的属性(读取“目标”)
2022-09-18
734
实际上我是 React 的新手,所以解决这个错误对我来说太难了。我试图使用“/hotels”在单独的页面中显示酒店列表,但是当我输入这个时,它显示了我在下面提到的错误,我也尝试设置目的地的值,但没有用。但在 /hotels 之后,我也给出了 id 的路线路径,因此 /hotels/id 运行良好,但 /hotels 不起作用,显示空白页和错误。
App.js
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import Home from './pages/home/Home';
import List from './pages/list/List';
import Hotel from './pages/hotel/Hotel';
function App() {
return (
<BrowserRouter>
<Routes>
<Route path='/' element={<Home />} />
<Route path='/hotels' element={<List />} />
<Route path='/hotels/:id' element={<Hotel />} />
</Routes>
</BrowserRouter>
);
}
export default App;
List.js
import './list.css';
import Navbar from '../../components/navbar/Navbar';
import Header from '../../components/header/Header';
import { useLocation } from 'react-router-dom';
import { useState } from 'react';
import { format } from 'date-fns';
import { DateRange } from 'react-date-range';
import SearchItem from '../../components/searchItem/SearchItem';
const List = () => {
const location = useLocation();
const [destination, setDestination] = useState(location.state.destination);
const [date, setDate] = useState(location.state.date);
const [openDate, setOpenDate] = useState(false);
const [options, setOptions] = useState(location.state.options);
return (
<div>
<Navbar />
<Header type='list' />
<div className='listContainer'>
<div className='listWrapper'>
<div className='listSearch'>
<h1 className='lsTitle'>Search</h1>
<div className='lsItem'>
<label>Destination</label>
<input placeholder={destination} type='text' />
</div>
<div className='lsItem'>
<label>Check-in Date</label>
<span onClick={() => setOpenDate(!openDate)}>{`${format(
date[0].startDate,
'MM/dd/yyyy'
)} to ${format(date[0].endDate, 'MM/dd/yyyy')}`}</span>
{openDate && (
<DateRange
onChange={(item) => setDate([item.selection])}
minDate={new Date()}
ranges={date}
/>
)}
</div>
<div className='lsItem'>
<label>Options</label>
<div className='lsOptions'>
<div className='lsOptionItem'>
<span className='lsOptionText'>
Min price <small>per night</small>
</span>
<input type='number' className='lsOptionInput' />
</div>
<div className='lsOptionItem'>
<span className='lsOptionText'>
Max price <small>per night</small>
</span>
<input type='number' className='lsOptionInput' />
</div>
<div className='lsOptionItem'>
<span className='lsOptionText'>Adult</span>
<input
type='number'
min={1}
className='lsOptionInput'
placeholder={options.adult}
/>
</div>
<div className='lsOptionItem'>
<span className='lsOptionText'>Children</span>
<input
type='number'
min={0}
className='lsOptionInput'
placeholder={options.children}
/>
</div>
<div className='lsOptionItem'>
<span className='lsOptionText'>Room</span>
<input
type='number'
min={1}
className='lsOptionInput'
placeholder={options.room}
/>
</div>
</div>
</div>
<button>Search</button>
</div>
<div className='listResult'>
<SearchItem />
<SearchItem />
<SearchItem />
<SearchItem />
<SearchItem />
<SearchItem />
<SearchItem />
<SearchItem />
<SearchItem />
</div>
</div>
</div>
</div>
);
};
export default List;
and the errors are
Uncaught TypeError: Cannot read properties of null (reading 'destination')
at List (List.jsx:12:1)
at renderWithHooks (react-dom.development.js:16305:1)
at mountIndeterminateComponent (react-dom.development.js:20074:1)
at beginWork (react-dom.development.js:21587:1)
at beginWork$1 (react-dom.development.js:27426:1)
at performUnitOfWork (react-dom.development.js:26557:1)
at workLoopSync (react-dom.development.js:26466:1)
at renderRootSync (react-dom.development.js:26434:1)
at recoverFromConcurrentError (react-dom.development.js:25850:1)
at performConcurrentWorkOnRoot (react-dom.development.js:25750:1
2个回答
正如您提到的
location.state.destination
,我希望路由使用
useNavigate
以编程方式完成,并且您正在发送数据。
const [destination, setDestination] = useState(location.state.destination);
确保您发送的数据如下
navigate('/hotels',{state:{location:"USA",date:xyz,options:{}}});
Kaneki21
2022-09-18
发生这种情况的原因是,您尝试直接转到 /hotels 页面,而不在搜索框中输入任何内容。由于您正在使用搜索框值初始化状态值,因此您应该填写搜索框,然后使用搜索按钮转到 /hotels 页面。
Abrar Haque
2023-03-16