0

I am about to build a small WebApp with react for personal use and for some experience :)

I want to work with the users input -> depending on how many positions he will fill out, the amount of possible "tabs" or "buttons" will vary

For this example I've created an array state called "data".

I'd like to ask you if theres a better way to handle this situation, and how would you deal with the handling of an action if one of these buttons will be clicked ?

simple vanilla event listener and then continue in react within that function or comepletly different ?

Shall I create an onClick={this.someFunc} simultaniously to every button that gets created?

Heres my tiny react code: http://pastebin.com/eseYdr3G

2
  • Please include the code in the question rather than linking out to it. Also, it's not clear what you're asking. You can just create a loop that creates the number of Components needed. I'd expect that you'd have a handler for onClick with the button and then perform the necessary action. Commented Mar 11, 2015 at 12:19
  • Thank you for your Anwer. I tried to paste the code sample in here but it wouldn't let me. Always told me "it looks like you have some bad formated code in here" or so. I ve reedited my Question. Commented Mar 11, 2015 at 13:04

1 Answer 1

3

I think you've got the right idea. When you create the button, you could set its id to the right name, and set its click callback, like so:

return <button id={i[0]} onClick={this.handleClick}>{i[0]}</button>

(In this case the ID is the same as the name, but you can do it however you like.)

Then you'd have your handler inside the class:

handleClick: function(e) {
  e.preventDefault();
  var buttonName = e.target.id;
  // buttonName is the name of the button clicked
}
Sign up to request clarification or add additional context in comments.

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.