0

I'm trying to map across an array stored in props in my React component. The problem is that I keep getting an error stating that 'step is not defined', even when I have an array of steps defined such as below. Any thoughts on what I am doing incorrectly? Thanks in advance!

render() {
const protocol = this.props.protocol;
const steps = protocol.steps;
let stepList = null;

if (steps != null) {
  stepList = <ul>
    steps.map(
      step => <li>{step.title}</li>
    )
  </ul>;
} else {
  stepList = 'Do not display list';
}

return (
  <div className="protocols-detail">
    List of steps for {protocol.title}
    { stepList }

  </div>
);

}

enter image description here

2 Answers 2

1

Issue is in this line, you forgot to use {}, to use any js code inside HTML element always use {} try this:

stepList = <ul>
    {
       steps.map(
         (step,i) => <li key={i}>{step.title}</li>
       )
    }
  </ul>;

One more thing always assign the unique key to each items when creating the ui elements dynamically in the loop.

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

2 Comments

Thank you! I am still in the learning process and I appreciate the help.
glad, to help you :), if u r beginner in reactjs then read this one, atleast basic things, it will help you alot :) facebook.github.io/react/docs/hello-world.html
1

You need to wrap steps.map in { } (inside <ul></ul> tags)

so it should be:

render() {
const protocol = this.props.protocol;
const steps = protocol.steps;
let stepList = null;

if (steps != null) {
  stepList = <ul>
    {steps.map(
      step => <li>{step.title}</li>
    )}
  </ul>;
} else {
  stepList = 'Do not display list';
}

return (
  <div className="protocols-detail">
    List of steps for {protocol.title}
    { stepList }

  </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.