0

I have some data with questions and I want to print the list of them and before every question there should be an icon. There are three types of icons, depending of question type.

How I can do it? That way I get an error "Expected an assignment or function call and instead saw an expression". I tried to fix it in many ways and nothing works :(

render() {
 const questions = questionsToChoose.map(q => {
      const icon = q => {
        if (q.isTrueFalse === true && q.isMultiple === false) {
          return 'a';
        }
        else if (q.isTrueFalse === true && q.isMultiple === true) {
          return 'b'
        }
        else if (q.isTrueFalse === false && q.isMultiple === false) {
          return 'c'
        }     
      };
      <div key={q.id}>
        <p>{icon} {q.content}</p>
      </div>
    });

   return(
    <div>{questions}</div>
   ) 
}
1
  • Can you add the service data (questionsToChoose)? Commented May 6, 2020 at 17:05

1 Answer 1

2

icon is defined as a function, you need to call it. Also moving it outside of render will improve performance

icon = q => {
  if (q.isTrueFalse === true && q.isMultiple === false) {
    return 'a';
  }
  else if (q.isTrueFalse === true && q.isMultiple === true) {
    return 'b'
  }
  else if (q.isTrueFalse === false && q.isMultiple === false) {
    return 'c'
  }     
};

render() {
 const questions = questionsToChoose.map(q => (

      <div key={q.id}>
        <p>{this.icon(q)} {q.content}</p>
      </div>
    ));

   return(
    <div>{questions}</div>
   ) 
}
Sign up to request clarification or add additional context in comments.

2 Comments

It works, thank you! :) I only needed to also change {} for () in map function after removing icon function.
Glad to have helped. Yes I missed that in brackets thing in the post. Glad you figured it out yourself. :)

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.