JSX文件为什么在浏览器中不显示?

huangapple go评论76阅读模式
英文:

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 &#39;react&#39;;

function Admin() {
  const [products, setProducts] = useState([]);
  const [newProduct, setNewProduct] = useState(&#39;&#39;);

  const handleAddProduct = () =&gt; {
    if (newProduct !== &#39;&#39;) {
      setProducts([...products, newProduct]);
      setNewProduct(&#39;&#39;);
    }
  };

  const handleDeleteProduct = (index) =&gt; {
    const updatedProducts = [...products];
    updatedProducts.splice(index, 1);
    setProducts(updatedProducts);
  };

  return (
    &lt;div&gt;
      &lt;h2&gt;Admin Page&lt;/h2&gt;

      &lt;h3&gt;Add Product&lt;/h3&gt;
      &lt;input
        type=&quot;text&quot;
        value={newProduct}
        onChange={(e) =&gt; setNewProduct(e.target.value)}
      /&gt;
      &lt;button onClick={handleAddProduct}&gt;Add&lt;/button&gt;

      &lt;h3&gt;Product List&lt;/h3&gt;
      &lt;ul&gt;
        {products.map((product, index) =&gt; (
          &lt;li key={index}&gt;
            {product}
            &lt;button onClick={() =&gt; handleDeleteProduct(index)}&gt;Delete&lt;/button&gt;
          &lt;/li&gt;
        ))}
      &lt;/ul&gt;
    &lt;/div&gt;
  );
}

export default Admin;

and here is the main app

import React from &#39;react&#39;;
import &#39;./App.css&#39;;
import {BrowserRouter, Routes, Route} from &#39;react-router-dom&#39;;
import Layout from &#39;./components/Layout&#39;;
import Home from &#39;./pages/Home&#39;;
import About from &#39;./pages/About&#39;;
import Contact from &#39;./pages/Contact&#39;;
import Wishlist from &#39;./components/Wishlist&#39;;
import Login from &#39;./pages/Login&#39;;
import Register from &#39;./pages/Register&#39;;
import SingleProduct from &#39;./pages/SingleProduct&#39;;
import Cart from &#39;./components/Cart&#39;;
import Admin from &#39;./pages/Admin&#39;;


function App() {
  return (
    &lt;&gt;
    &lt;BrowserRouter&gt;
    &lt;Routes&gt;
    &lt;Route path=&quot;/&quot; element={&lt;Layout /&gt;}&gt;
            &lt;Route index element={&lt;Home /&gt;} /&gt;
            &lt;Route path=&quot;about-us&quot; element={&lt;About /&gt;} /&gt;
            &lt;Route path=&quot;contact&quot; element={&lt;Contact /&gt;} /&gt;
            &lt;Route path=&quot;wishlist&quot; element={&lt;Wishlist /&gt;} /&gt;
            &lt;Route path=&quot;login&quot; element={&lt;Login /&gt;} /&gt;
            &lt;Route path=&quot;register&quot; element={&lt;Register /&gt;} /&gt;
            &lt;Route path=&quot;product/:id&quot; element={&lt;SingleProduct /&gt;} /&gt;            
            &lt;Route path=&quot;cart&quot; component={&lt;Cart /&gt;} /&gt;
            &lt;Route path=&quot;admin&quot; component={&lt;Admin /&gt;} /&gt;
          &lt;/Route&gt;
    &lt;/Routes&gt;
    &lt;/BrowserRouter&gt;
    &lt;/&gt;
  );
}

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={&lt;Admin /&gt;},而应该像其他地方一样使用 element={&lt;Admin /&gt;}(除了 cart,我怀疑它也不起作用)。

英文:

I'm pretty sure it's because you have component={&lt;Admin /&gt;} when it should be element={&lt;Admin /&gt;} like all the rest (except cart which I suspect also doesn't work)

答案2

得分: 0

从父布局路线中移除路径。更新您代码的这部分:

&lt;Route element={&lt;Layout /&gt;}&gt;
英文:

Remove the path from Parent Layout Route. Update this part of your code:-

&lt;Route element={&lt;Layout /&gt;}&gt;

huangapple
  • 本文由 发表于 2023年5月11日 05:54:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定