Hi there,
I have an issue. When I go to each room page, I mean “http://localhost:3000/room/1” or “/room/2” and then I see My object. But When I refresh the pages the objects disappear. And show ERROR.
This is my App.js code:
import React from 'react'
import Home from './components/Home'
import { Route, Routes } from 'react-router-dom'
import Navbar from './components/Navbar'
import RoomList from './components/RoomList'
import DataGrid from './components/DataGrid'
import RoomOne from './components/RoomOne'
import RoomTwo from './components/RoomTwo'
import RoomThree from './components/RoomThree'
import RoomFour from './components/RoomFour'
import RoomFive from './components/RoomFive'
import RoomSix from './components/RoomSix'
import RoomSeven from './components/RoomSeven'
import RoomEight from './components/RoomEight'
const App = () => {
return (
<>
<Navbar />
<Routes>
<Route path='/' element={<Home />} />
<Route path='/allRooms' element={<Home />} />
<Route path='/rooms' element={<RoomList />} />
<Route path='/dataGrids' element={<DataGrid />} />
<Route path='/room'>
<Route path='1' element={<RoomOne />} />
<Route path='2' element={<RoomTwo />} />
<Route path='3' element={<RoomThree />} />
<Route path='4' element={<RoomFour />} />
<Route path='5' element={<RoomFive />} />
<Route path='6' element={<RoomSix />} />
<Route path='7' element={<RoomSeven />} />
<Route path='8' element={<RoomEight />} />
</Route>
<Route path="*" element={<h1>Not found | Please go home</h1>} />
</Routes>
</>
)
}
export default App
it is not recommended to create and destroy the canvas, the threejs webglrenderer etc. this puts so much stress onto the browser and the GC, so much time and memory wasted. this is react, so you can have routes within the canvas, exchange contents and meshes, don’t unmount and re-mount the whole thing from scratch, route changes will be immediate as they should be.
this will also eliminate hundreds of lines of code because all the canvas content are doubled in each room. lights, controls etc, all that can stay mounted, only the room should load.
another hint, use lazy loading, there’s no reason to load all the rooms at once:
as for your problem, when i use this url: “./models/room1.glb” and hit refresh, i get
Could not load ./models/room1.glb: Unexpected token '<', "<!DOCTYPE "... is not valid JSON)
when i use “/models/room1.glb” it works.
keep in mind that routing changes the url of your navbar and that also affects how and from where you can fetch assets. i don’t know the specifics of your app but a 404 is a sure sign that paths are incorrect.
I can’t wrap my app.js (route) with canvas.
Its says “Canvas is not part of the THREE namespace! Did you forget to extend? See: React Three Fiber Documentation”
But it says this " Module not found: Error: You attempted to import /models/room1.glb which falls outside of the project src/ directory. Relative imports outside of src/ are not supported. You can either move it inside src/, or add a symlink to it from project’s node_modules/."
Do I need to move the object to scr folder ? I mean create a new folder and put the raw glb objects?
Right now glb objects are in public folder
that’s why you get that error. everything inside <Canvas> is threejs, you can’t mount dom elements in there. you are supposed to mount the threejs contents into the canvas, but your room components have a full <Canvas> in them. delete all these components. only leave /src/assets/Room*.js. and then you can just link them via routes.
i don’t understand, it’s your project, your canvas worked before, you placed it into your css grids. so why would you not place the new canvas in app.js into the same grid? your canvas had zero height. canvas will take 100% width and 100% height of the next absolute or relative parent, like any other dom node.
i think it would be good if you took some more time before relying on external help, i almost feel bad for helping you because this will just damage your learning. try to be a little more inspective: something doesn’t work, why could that be, why did it work before, what is the difference between the new code and the old, etc.
Thanks for helping Man. Please don’t get it wrong I’m new. I re-read all your comments and did the code by myself. That’s why I earlier mark your comment as a solution.
I got the solution path from this. Here’s my latest code. https://codesandbox.io/s/nooooo----------nooooo-0hij48?file=/src/App.js
Please check it and give a review :)) Its possible because you help me :))