1

I am trying to create array of buttons as shown below in the code section. I am using sandbox. I'm getting the error posted below

code:

import React from "react";
import ReactDOM from "react-dom";
import createReactClass from "create-react-class";

var arrButtons = [];
var buttonStyle = {
    margin: '10px 10px 10px 0'
};

class ButtonClicks extends React.Component {
    constructor(props) {
        super(props);
        this.onClickFunction = this.onClickFunction.bind(this);
    }
    onClickFunction() {
      console.log("hello world from button: " + i);
    };

    render() {
        return (
            for (let i = 0; i < 10; i++) {
                var button = <button
                    style={buttonStyle}
                    onClick={onClickFunction}>
                    p.name
                </button>
                arrButtons.push(button);
            }
        );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<ButtonClicks />, rootElement);
export default ButtonClicks;

error

/src/index.js: Unexpected token (21:4)
3
  • Would you post error messages? Commented Jul 31, 2018 at 13:36
  • Wait, this post's tag is react-native but the code in question is react-dom. Might want to edit this. Commented Jul 31, 2018 at 13:51
  • Anyways, check my answer. Your approach is already correct but render only accepts 1 element. So, I've moved the for loop and put the buttons inside a div. Commented Jul 31, 2018 at 14:09

1 Answer 1

1

Your approach is correct. BUT! React's render only accepts 1 element (which child can have as many elements as you need it). So you need to wrap those buttons inside a div. That's about the only change I made aside of moving your for loop outside return.

var arrButtons = [];
var buttonStyle = {
    margin: '10px 10px 10px 0'
};

class ButtonClicks extends React.Component {
  constructor(props) {
    super(props);
    this.onClick = this.onClick.bind(this);
  }
  
  onClick() {
    console.log("hello world");
  }

  render() {
    for (let i = 0; i < 10; i++) { //Moved your loop outside render()'s return
      arrButtons.push(<button style={buttonStyle} onClick={this.onClick}>{i}</button>)
    }
    return (
      <div>
        {arrButtons} {/*Very important to wrap the buttons inside a div*/}
      </div>
    );
  }
}

ReactDOM.render( <ButtonClicks/> , document.getElementById("root"));
<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="root"></div>

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

2 Comments

thanks i would like to pass the index i to onClickFunction so that when button 3 is clicked, i should get: hello world from button 3....would you please help m doing it
onClick={() => this.onClick(i)}

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.