I am working on a web app that is consisted of few pages and have configured routes with react-route-dom. On each page, I have some shared components such as Navbar and I want them to be styled differently on other pages except the Home page.
One option might be to look at the pathname returned by the useLocation hook inside the Nav component and apply different styles depending on the path.
I assume you have the Nav component outside the Routes so it is applied to all pages, or using some layout component? Or do you have more than one Nav component?
The Route for the SharedLayout Component is the parent Route for all other Routes and it’s visible on all the other pages. The SharedLayout contains other components including the Nav.
import {BrowserRouter, Routes, Route} from 'react-router-dom'
import Home from './pages/Home'
import About from './pages/About'
import Services from './pages/Services'
import Contact from './pages/Contact'
import Error from './pages/Error'
import SharedLayout from './pages/SharedLayout'
import Footer from './components/Footer'
function App() {
return (
<div className="App">
<BrowserRouter>
<Routes>
<Route path='/' element={<SharedLayout />} >
<Route index element={<Home />} />
<Route path='/about' element={<About />} />
<Route path='/services' element={<Services />} />
<Route path='/contact' element={<Contact />} />
<Route path='*' element={<Error />} />
</Route>
</Routes>
</BrowserRouter>
<Footer />
</div>
);
}
export default App;
Here is my Nav Component and it is this one I want to give a different style on other pages.
Yes I did and I tried to implement it but I’m stuck. If you look at my StyledNavbar Component, you’ll notice that I’m getting the location and pathname, each into a constant.
While waiting until such time that you will be free, I will be focusing on other parts of the project.
@lasjorg could you please help me out on this one? I’ve been working on the JavaScript Calculator and I’m currently stuck.
I’ve been trying to code the application so that when I input a number and if there is a number greater than 0 on the display, the inputted number will be appended to the existing one on the display. As much as I have tried, I don’t seem to be making any progress.
It’s addDisplayValue I was talking about. I want to be able to append a new value to the display if I alredy have an existing value there.
I only used the defaultProps to set a default value for display but It’s not working still. Yes, I’m checking the default initial value and if it’s between 1 and 9 both ends inclusive, the newly entered value from the keypad should be appended to the existing value. And this is what I’ve been struggling with. Everytime I entered a new value it set the display to that new value replacing the existing value.
I want to actually return the currentState + previousState if the value in the display meets the if condition.
I actually wanted to push a new entered value into the array and somehow convert it to string. i was actually trying different approaches since I was confused. [].push(value) wasn’t my initial code.
I guess I was just confused by the naming. The state value isn’t really a default value if it can change. Sure you can start it out by setting it to a default value but calling something that can be changed by user input a default value is misleading I think.
If the state value is a string you can use normal string concatenation if it’s an array you can use the current value and append the new value, usually using spread is a good approach [...currentState, newValue], if you want the array as a string you can use .join()
Hi @lasjorg; can I have your input on this one?
The problem I’m trying to solve this time is being able to perform arithmetic operations on the inputted numbers.
This is my algorithm:
take the input, split into array
find each operator in the array and its position
find numbers to the left of each operator as well as the last number
after the last operator
check the type of operators(+, -, /, *)
perform operation of an operator with the highest order
then next in order and so on
finally, show result
But I was stuck somwhere at point 3. Below is my code. I’m trying to solve it with a regular function before taking it to my JavaScript Calculator React App.