Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

99
Views
Set local variable in method inside the html in reactjs

I have this method render, within which I am creating a local variable category. I need to set the value of this variable inside the html in return statement using JSX.

I am using curly brackets to assign the value to the local variable, but this is not working for me. eg {category='business'}

This is what I am doing currently

export default class App extends Component {
  render() {
    let category = 'about';
    return (
      <Router>
        <div>
          <NavBar/>
          <Switch>
            <Route path="/about">
            </Route>
            <Route path="/business">
              {category='business'}
            </Route>
            <Route path="/entertainment">
              {category='entertainment'}
            </Route>
            <Route path="/general">
              {category='general'}
            </Route>
            <Route path="/health">
              {category='health'}
            </Route>
            <Route path="/science">
              {category='science'}
            </Route>
            <Route path="/sports">
              {category='sports'}
            </Route>
          </Switch>
          <News pageSize={5} country="in" category={category}/>
        </div>
      </Router>
    )
  }
}

This one instead shows the category on screen.

Note : I know there are multiple ways in which I can achieve this but I wanted to know how to set local variable within my method

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  1. First of all, look into functional components.
  2. A Route component expects a component, not logic. Handle the actual variable change in that component or elsewhere via react-router's useLocation hook.

What you're attempting to do is to change the state of the variable. Cue React's useState and useEffect hooks (the component needs to be a functional component for hooks to work).

First import the hook:

import { useState } from 'react';

Then, in your case:

const [category, setCategory] = useState('about') // initial value

and when you need to update it:

setCategory('science') // new value
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!