0

Hi guys i am currently learning the React library and JSX but im stuck bc i dont understand what they want me to do here. You guys are my last solution: here is the code ... Use .renderSortByOptions() to sort the businesses by their options (If your wondering what im doing : i am currently trying to make something like Yelp using their API).. i know this site is not for debugging and i usually never ask for stuff like this here but i wanna keep learning and beeing stuck for so long is very demotivating

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

const sortByOptions{
  'Best Match': 'best_match',
  'Highest Rated': ' rating',
  'Most Reviewed': 'review_count'
}

class SearchBar extends React.Component{
  renderSortByOptions(){
    return Object.keys(sortByOptions).map(sortByOption =>{
      let sortByOptionValue = sortByOptions[sortByOption];
      return <li key=sortByOptionValue ></li>;
    }
  });

  render(){
    return(
      <div className="SearchBar">
  <div className="SearchBar-sort-options">
    <ul>
      <!-- Use .renderSortByOptions() to sort the businesses by their options -->
    </ul>
  </div>
  <div className="SearchBar-fields">
    <input placeholder="Search Businesses" />
    <input placeholder="Where?" />
  </div>
  <div className="SearchBar-submit">
    <a>Lets Go</a>
  </div>
</div>
    );
  }
  }
}

2
  • what do you want the li to display? the values? Commented Jan 17, 2018 at 19:12
  • Don't you need an = sign after const sortByOptions? Commented Jan 17, 2018 at 19:16

4 Answers 4

2

They just mean to call renderSortByOptions where that comment is.

Since it is in the middle of other JSX though, you will need to wrap it with curly braces, like {this.renderSortByOptions()}

Sign up to request clarification or add additional context in comments.

3 Comments

Also it does look like you are missing a = where the sortByOptions variable is defined. const sortByOptions{ should be const sortByOptions = {
you mean {} bc its regular javascript?
I mean in order to escape javascript from JSX you have to wrap it in {}.
0

You need to call it like {this.renderSortByOptions()} and also correct the other syntactical errors. Check this snippet

const sortByOptions = {
  'Best Match': 'best_match',
  'Highest Rated': ' rating',
  'Most Reviewed': 'review_count'
}

class SearchBar extends React.Component{
  renderSortByOptions(){
    return Object.keys(sortByOptions).map(sortByOption =>{
      let sortByOptionValue = sortByOptions[sortByOption];
      return <li key={sortByOptionValue} >sortByOptionValue</li>;
    })
  };

  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>
    );
  }
}

ReactDOM.render(<SearchBar/>, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"/>

Comments

0

This is the working code : you also need to bind the function and also there were few brackets extra.

const sortByOptions ={
  'Best Match': 'best_match',
  'Highest Rated': ' rating',
  'Most Reviewed': 'review_count'
}

class SearchBar extends React.Component {

  renderSortByOptions() {
    return Object.keys(sortByOptions).map(sortByOption => {
      let sortByOptionValue = sortByOptions[sortByOption];
      return <li key={sortByOptionValue} ></li>
    })
  }

  render(){
    this.renderSortByOptions = this.renderSortByOptions.bind(this);
    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>
    );
  }
  }

check snippet https://codesandbox.io/s/93xp5w1054

Comments

0

There was a lot of errors, You should check your console before asking a question to resolve the syntax issues.

Here is an optimised version where you are passing your sort options to your <SearchBar /> component to make it reusable.

The main issue you had with your question was you needed to execute your method {this.renderSortByOptions()}

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

const app = document.querySelector('#app')
const sortByOptions = {
  'Best Match': 'best_match',
  'Highest Rated': ' rating',
  'Most Reviewed': 'review_count'
}

class SearchBar extends React.Component{
  renderSortByOptions(options) {
    return Object.keys(options).map(
      key => <li key={key}>{options[key]}</li>
    )
  }

  render(){
    return (
      <div className="SearchBar">
        <div className="SearchBar-sort-options">
          <ul>
            {this.renderSortByOptions(this.props.options)}
          </ul>
        </div>
        <div className="SearchBar-fields">
          <input placeholder="Search Businesses" />
          <input placeholder="Where?" />
        </div>
        <div className="SearchBar-submit">
          <a>Lets Go</a>
        </div>
      </div>
    );
  }
}

ReactDOM.render(
  <SearchBar options={sortByOptions} />,
  app
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<article id="app"></article>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.