解決React路由屬性中取得undefined的問題
<p>我想把一些屬性(<code>loading</code>和<code>getContacts</code>)送到ReactJS中的一個元件。我使用路由來取得每個路徑,但是目標元件中的結果是undefined。這是怎麼回事? </p>
<pre class="brush:php;toolbar:false;">const App = () => {
const [getContacts , setContacts] = useState([]);
const [loading , setLoading] = useState(false);
return(
<div className='App'>
<Navbar/>
<Routes>
<Route path='/' element = {<Navigate to ="/contacts"/>}/>
<Route path='/contacts' loading= {loading} contacts= {getContacts} element= {<Contacts/>} />
<Route path="/contacts/add" element = {<AddContact/>}/>
<Route path = "/contacts/:contactId" element = {<ViewContact/>}/>
<Route path = "/contacts/edit/:contactId" element = {<EditContact/>}/>
<Route path="*" element={
<div className='text-light py-5'>
Not Found!
</div>}
/>
</Routes>
</div>
);
export default App;</pre>
<p>在Contact.jsx中,我有:</p>
<pre class="brush:php;toolbar:false;">const Contacts = ({ contacts, loading }) => {
return (
<>
<div>{contacts}</div>
<div>{loading}</div>
</>
);
};
export default Contacts;</pre>
<p>但是它們都是undefined。 </p>