Ishan Patel
Ishan Patel

Reputation: 6091

How to access return value of a method in classname field in ReactJS?

I have following function outside the component

  function getSortByClass(sortByOption){
    if (this.state.sortBy === sortByOption) {
      return 'active';
    }
    else {
      return '';
    }
  }

I have a component which is returning following function.

  return Object.keys(sortByOptions).map(sortByOption => {
    let sortByOptionValue = sortByOptions[sortByOption];

    return <li className={} key={sortByOptionValue} 
 onClick={ handleSortByChange.bind(this, sortByOptionValue)}> {sortByOption} </li>;
  });

I want to know how I can access the return value of the getSortByClass function in <li> tag classname's value.

Here's the full component code.

import React from 'react';
import './SearchBar.css';

const sortByOptions = {
  'Best Match': 'best_match',
  'Highest Rated': 'rating',
  'Most Reviewed': 'review_count'
}
  function getSortByClass(sortByOption){
    if (this.state.sortBy === sortByOption) {
      return 'active';
    }
    else {
      return '';
    }
  }

export class SearchBar extends React.Component{
    renderSortByOptions(){
      const that = this;

    function handleSortByChange(sortByOption){
      this.setState({ sortBy: sortByOption});
      }

      return Object.keys(sortByOptions).map(sortByOption => {
        let sortByOptionValue = sortByOptions[sortByOption];
        return <li className={} key={sortByOptionValue} onClick={ handleSortByChange.bind(this, sortByOptionValue)}> {sortByOption} </li>;
      });
    }


    constructor(props) {
        super(props);
        this.state = {
          term: '',
          location: '',
          sortBy: 'best_match',
        };
    }
    render(){
      return (
      <div className="SearchBar">
        <div className="SearchBar-sort-options">
          <ul>
            {this.renderSortByOptions()}
          </ul>
        </div>
        <div className="SearchBar-fields">
          <input placeholder="Search Businesses" />
          <input placeholder="Where?" />
        </div>
        <div className="SearchBar-submit">
          <a>Lets Go</a>
        </div>
        </div>
      );
    }
  }

  export default SearchBar;

Upvotes: 0

Views: 16726

Answers (1)

Dakota
Dakota

Reputation: 1274

I don't know if what you are trying to do is possible but it's definately not a good idea. Just pass the part of state you are using as a parameter to the function.

  function getSortByClass(sortBy, sortByOption){
    if (sortBy === sortByOption) {
      return 'active';
    }
    else {
      return '';
    }
  }

then change your call to

    return <li className={} key={sortByOptionValue} onClick={ handleSortByChange(this.state.sortBy, sortByOptionValue)}> {sortByOption} </li>;

If the function doesn't have to be out of scope just put it in the component like @mersocarlin said.

Edit using setState:

  getSortByClass(sortByOption){
    if (this.state.sortBy === sortByOption) {
      this.setState({styleClass: 'active' });
    }
    else {
      this.setState({styleClass: '' });
    }
  }

Upvotes: 1

Related Questions