0

I need to display a dropdownlist element with some optgroup labels in my react components.

So I created this method :

 onRenderCategories: any = () => {

        var scope = "";
        var rendu = "";

        for (var scopes in this.props.categories) {
            var ensemble: Array<CategoryTemplateInfos> = this.props.categories[scopes];
            if (ensemble.length == 0) continue;
            scope = ensemble[0].scope;
            rendu += "<optgroup label=" + scope + ">";
            for (var j = 0; j < ensemble.length; j++) {
                var categorie: string = ensemble[j].label;
                var id_categorie: string = ensemble[j].id;
                rendu += "<option value=" + id_categorie+">" + categorie+"</option>";
            }
             rendu += "</optgroup>";
        }
        return rendu;
    }

the output of this method is as excepted ie the html content to put here

 <select >
       <option value="">--Please select a category--</option>
       {this.onRenderCategories()} //here
  </select>

It didn't work!

So how can I fix this issue to convert a string to HTML elements ?

Thanks,

4
  • 2
    why not creating components instead of string markup? Commented Sep 13, 2018 at 15:02
  • I tried to convert this method to a component but I get the same result Commented Sep 13, 2018 at 15:08
  • is this a typo or you forgot to invoke the onRenderCategories method? {this.onRenderCategories} Commented Sep 13, 2018 at 15:08
  • it is a typo, I correct this mistake Commented Sep 13, 2018 at 15:19

2 Answers 2

1

Try this:

onRenderCategories: any = () => {

    let scope = "";
    let items:JSX.Element[] = [];

    for (var scopes in this.props.categories) {
        let ensemble: Array<CategoryTemplateInfos> = this.props.categories[scopes];
        if (ensemble.length == 0) continue;
        scope = ensemble[0].scope;
        let options = ensemble.map(item => <option value={item.id} key={item.id}>{item.label}</option>)
        let itemstmp = <optgroup label={scope} key={scope}>{options}</optgroup>
        items.push(itemstmp);
    }
    return items;
}


<select >
       <option value="">--Please select a category--</option>
       {this.onRenderCategories()} //here
</select>
Sign up to request clarification or add additional context in comments.

1 Comment

@Lamloumi Afif edited to make sure the code is correct. Glad I could help.
0

React doesn't work with HTML, it works with JSX, which is only similar to HTML at first glance. In fact it is pure Javascript once JSX is processed by babel.

First of all, you need to invoke your function: {this.onRenderCategories()} - notice the parenthesis - you don't have those in your code. Then, in your categories function you need to switch to actual JSX, and not concat strings because that will not work.

I think the best thing is to go through official React tutorials explaining what React IS and how it works. After you read that, this question will resolve on its own.

So please check out the official documentation :)

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.