0
class App extends Component {
    render() {
        return (
            ........
            { let tags = this.state.tags;
            for (var key in tags) {
                <p className="h3 text-primary">{tags[key]}</p>
            }}
            .........
         )
    }
}

I have error: Failed to compile - Unexpected token. I don't understand where I have mistake. Help me please.

4
  • where in the component is this block of code present? Commented Jun 17, 2018 at 21:47
  • I edited question Commented Jun 17, 2018 at 21:51
  • points to the line with let Commented Jun 17, 2018 at 21:53
  • Please provide a minimal reproducible example. I suggest creating a new component and build up to just enough code to reproduce the problem you are encountering. Commented Jun 18, 2018 at 0:12

2 Answers 2

1

replace

{
 let tags = this.state.tags;
 for (var key in tags) {
   <p className="h3 text-primary">{tags[key]}</p>
 }
}

with {Object.keys(tags).map(key => <p key={key} className="h3 text-primary">{tags[key]}</p>)};

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

4 Comments

it's not work, I tried so to do. Uncaught TypeError: Cannot read property 'undefined' of undefined
post the whole component, is your state defined?
I published the full component
There is no state in your component. How do you suppose to get tags from this.state?
0

First of all you don't have a state in your app. You can't use this.state.something if there is not a state.

Secondly, do not use for loops in your render function like this. There are better ways to do this as suggested in the other answer. But if you really want to do this:

class App extends React.Component {
  state = {
    tags: {
      foo: "somefoo",
      bar: "somebar",
      baz: "somebaz"
    }
  };

  render() {
    const { tags } = this.state;
    const arr = [];
    for (var key in tags) {
      arr.push(<p className="h3 text-primary">{tags[key]}</p>);
    }
    return (
      arr
    );
  }
}

But, render method is so crowded, let's make a little bit cleaning.

class App extends React.Component {
  state = {
    tags: {
      foo: "somefoo",
      bar: "somebar",
      baz: "somebaz"
    }
  };

  getTags = () => {
    const { tags } = this.state;
    const arr = [];
    for (var key in tags) {
      arr.push(<p className="h3 text-primary">{tags[key]}</p>);
    }
    return arr;
  }

  render() {
    return (
      this.getTags()
    );
  }
}

But there are better ways to iterate over stuff like .map or Object(keys) then .map for your example as suggested.

class App extends React.Component {
  state = {
    tags: {
      foo: "somefoo",
      bar: "somebar",
      baz: "somebaz"
    }
  };

  render() {
    const { tags } = this.state;
    return (
      Object.keys(tags)
        .map(key => <p key={key}>{tags[key]}</p>)
    );
  }
}

But again, you can use a separate function to get a cleaner structure for your render method.

class App extends React.Component {
  state = {
    tags: {
      foo: "somefoo",
      bar: "somebar",
      baz: "somebaz"
    }
  };

  getTags = () => {
    const { tags } = this.state;
    return Object.keys(tags)
      .map( key => <p key={key}>{tags[key]}</p>)
  }

  render() {
    return (
      this.getTags()
    );
  }
}

Nice and clean! Don't forget if you need other JSX in here you have to use {} to use javascript expressions. For example what if you want a wrapper div top of your p's?

class App extends React.Component {
  state = {
    tags: {
      foo: "somefoo",
      bar: "somebar",
      baz: "somebaz"
    }
  };

  getTags = () => {
    const { tags } = this.state;
    return Object.keys(tags)
      .map( key => <p key={key}>{tags[key]}</p>)
  }

  render() {
    return (
      <div>
        { this.getTags() }
      </div>
    );
  }
}

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.