score:0
so my question is, is there any way i could do the same using my original folder structure. or more specifically, is thery any way i could do res.render(oneofthereactjsroutehere).
not really. if you're looking at server-side rendering of react then the usual approach is next.js (which means uses next.js' server and next.js' router).
but there is one particular case, i want to login as admin and set the session accordingly and again i can check if the admin is logged in using useeffect in frontend to fetch data from backend and do the things. but this will be tedious to in every reactjs page, as i have to check if admin is logged in many different pages.
that wouldn't help - since client side navigation would skip getting the route from the server.
abstract the logic into something reusable instead.
<route
path="/admin"
element={
<requiresrole role="admin">
<studentdashboard />
</requiresrole>
}
/>
and then have a component like
const requiresrole = ({ role, children }) => {
const roles = usemyauthenticationhook();
if (!roles.includes(role)) {
return /* a redirect to a login page or an error message or something */
}
return children;
}
Source: stackoverflow.com
Related Query
- How to res.render react app routes from express js?
- How does Express and React Routes work on initial GET request from browser?
- How to get an image link from a JSON to render in a react app
- How to send react app to client with express when you have routes that are authenticated and some that are not?
- How to get a response from express server about an error using axios in react app
- How do I render a list from Json data in express js, then render it using react and map()?
- How to get data from backend express server to frontend react app and vice versa
- How can I render HTML from another file in a React component?
- How do I render Markdown from a React component?
- How to get the data from React Context Consumer outside the render
- How to pass the match when using render in Route component from react router (v4)
- How can I pass data from express server to react views?
- how to share environment variables between react app and express js server hosting it as static site
- How to render a React app in a React app (nested React apps)
- How to run and debug a react app directly from VSCode?
- How to read console.log from a mounted component with Enzyme and Jest in Create React App
- How to post data from react to express
- How to send form data from React to express
- Create react app - how to copy pdf.worker.js file from pdfjs-dist/build to your project's output folder?
- How to make a npm package from Create React App /build folder?
- Access Denied Error from protected routes from react app hosted on AWS Amplify
- How to render a react component from a string name
- How to get CSRF from django in a separate React App
- How can I make my React app render Bootstrap tables with crud
- How can I access my react app from another pc in local network?
- Deploying React + Express App to Kubernetes. How to structure it with docker?
- How to render data received from a REST service in React Universal? (Next.js)
- How to inject data from the server in index.html in an ASP.NET Core 3 React app
- How to Fetch and Display an Image from an express backend server to a React js frontend?
- how to fetch and display images from database in react app
More Query from same tag
- How can I access key using value in ReactJs?
- React Router Dom v6 useNavigate to Conditional Path Directory After Login
- How to make header component know if user is logged in in React?
- Type of handleSubmit parameter in react-hook-form
- Redux - ES6 Updating Object Element in Array
- Second argument in reactjs function is undefined
- How to access a property of App() globally (in React App, from pyodide)
- onClick not working inside the pop up opened via React portals
- Using min-height and max-height at the same time
- React form pass headers to api
- Change button value using URI in React.js
- How do I pass children index number to a react component?
- React Hooks - Ref is not avaiable inside useEffect
- React JS shopping cart add functions
- onClick, Transfer values to another component
- get variables through their name in react
- ANTD - How to change input value when blur another input
- Can I avoid submission of a React form before the state of all controlled fields was set
- Why useState variable gets undefined inside try catch statement in an asynchrone function?
- Redux add item to an existing object in state duplicates entry
- Firebase storage getImageURL after uploading to after save URL in firestore(React)?
- React Hook is called in function that is neither a React function component nor a custom React Hook function
- Prevent FlatList from re-rendering entire list when state changes
- Fetch additional data for a single item in a list query
- fabricjs gives error while importing with reactjs
- Why isn't the Formik `touched` property being populated?
- Putting fetch function in a separate component
- Hello, how can I display a real time clock in React?
- React : Expected an assignment or function call and instead saw an expression
- React hooks check what state change caused re-render