英文:
Why JSX file doesn't appear in browser?
问题
I try to build e-commerce app using reactjs and i got a problem on frontend, the page is not showing in browser. This page is for admin page that can do CRUD on product.
here is Admin.jsx code
import React, { useState } from 'react';
function Admin() {
const [products, setProducts] = useState([]);
const [newProduct, setNewProduct] = useState('');
const handleAddProduct = () => {
if (newProduct !== '') {
setProducts([...products, newProduct]);
setNewProduct('');
}
};
const handleDeleteProduct = (index) => {
const updatedProducts = [...products];
updatedProducts.splice(index, 1);
setProducts(updatedProducts);
};
return (
<div>
<h2>Admin Page</h2>
<h3>Add Product</h3>
<input
type="text"
value={newProduct}
onChange={(e) => setNewProduct(e.target.value)}
/>
<button onClick={handleAddProduct}>Add</button>
<h3>Product List</h3>
<ul>
{products.map((product, index) => (
<li key={index}>
{product}
<button onClick={() => handleDeleteProduct(index)}>Delete</button>
</li>
))}
</ul>
</div>
);
}
export default Admin;
and here is the main app
import React from 'react';
import './App.css';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import Layout from './components/Layout';
import Home from './pages/Home';
import About from './pages/About';
import Contact from './pages/Contact';
import Wishlist from './components/Wishlist';
import Login from './pages/Login';
import Register from './pages/Register';
import SingleProduct from './pages/SingleProduct';
import Cart from './components/Cart';
import Admin from './pages/Admin';
function App() {
return (
<>
<BrowserRouter>
<Routes>
<Route path="/" element={<Layout />} >
<Route index element={<Home />} />
<Route path="about-us" element={<About />} />
<Route path="contact" element={<Contact />} />
<Route path="wishlist" element={<Wishlist />} />
<Route path="login" element={<Login />} />
<Route path="register" element={<Register />} />
<Route path="product/:id" element={<SingleProduct />} />
<Route path="cart" component={<Cart />} />
<Route path="admin" component={<Admin />} />
</Route>
</Routes>
</BrowserRouter>
</>
);
}
export default App;
File placements is already correct but is still not showing on my browser.
Any help would be appreciated. Thank you
英文:
I try to build e-commerce app using reactjs and i got a problem on frontend, the page is not showing in browser. This page is for admin page that can do CRUD on product.
here is Admin.jsx code
import React, { useState } from 'react';
function Admin() {
const [products, setProducts] = useState([]);
const [newProduct, setNewProduct] = useState('');
const handleAddProduct = () => {
if (newProduct !== '') {
setProducts([...products, newProduct]);
setNewProduct('');
}
};
const handleDeleteProduct = (index) => {
const updatedProducts = [...products];
updatedProducts.splice(index, 1);
setProducts(updatedProducts);
};
return (
<div>
<h2>Admin Page</h2>
<h3>Add Product</h3>
<input
type="text"
value={newProduct}
onChange={(e) => setNewProduct(e.target.value)}
/>
<button onClick={handleAddProduct}>Add</button>
<h3>Product List</h3>
<ul>
{products.map((product, index) => (
<li key={index}>
{product}
<button onClick={() => handleDeleteProduct(index)}>Delete</button>
</li>
))}
</ul>
</div>
);
}
export default Admin;
and here is the main app
import React from 'react';
import './App.css';
import {BrowserRouter, Routes, Route} from 'react-router-dom';
import Layout from './components/Layout';
import Home from './pages/Home';
import About from './pages/About';
import Contact from './pages/Contact';
import Wishlist from './components/Wishlist';
import Login from './pages/Login';
import Register from './pages/Register';
import SingleProduct from './pages/SingleProduct';
import Cart from './components/Cart';
import Admin from './pages/Admin';
function App() {
return (
<>
<BrowserRouter>
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<Home />} />
<Route path="about-us" element={<About />} />
<Route path="contact" element={<Contact />} />
<Route path="wishlist" element={<Wishlist />} />
<Route path="login" element={<Login />} />
<Route path="register" element={<Register />} />
<Route path="product/:id" element={<SingleProduct />} />
<Route path="cart" component={<Cart />} />
<Route path="admin" component={<Admin />} />
</Route>
</Routes>
</BrowserRouter>
</>
);
}
export default App;
File placements is already correct but is's still not showing on my browser.
Any help would be appreciated. Thank you
答案1
得分: 1
我相当肯定这是因为你在这里使用了 component={<Admin />}
,而应该像其他地方一样使用 element={<Admin />}
(除了 cart
,我怀疑它也不起作用)。
英文:
I'm pretty sure it's because you have component={<Admin />}
when it should be element={<Admin />}
like all the rest (except cart
which I suspect also doesn't work)
答案2
得分: 0
从父布局路线中移除路径。更新您代码的这部分:
<Route element={<Layout />}>
英文:
Remove the path from Parent Layout Route. Update this part of your code:-
<Route element={<Layout />}>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论