0
onClick={ this.props.remove(this.props. + this.props.title) }

How can I accomplish to have this.props.custom_variable_name which is coming from the variable this.props.title This example doesnt work

2 Answers 2

2

Use bracket notation

onClick={() => this.props.remove(this.props[this.props.title])}
Sign up to request clarification or add additional context in comments.

3 Comments

That's nice and solves the OP's problem when you're dealing with named properties of objects, not actual variables. Let me add, that in case of variables, your solution is eval. Like in this.props.remove(eval(title));. It would work with properties too, but not only is it vastly slower, it's actually pretty risky (enabling code injection attacks if not safeguarded enough.)
return <Item remove={this.remove} title={item.title} key={idx} item={item} sortData={idx} {item.title}={idx} isDraggable={true} />; }.bind(this)); Ok, i have this line in React which is iterating with several Items , and I am trying to make this Item unique by passing {item.title} as parameter name, but it is giving me syntax error. How can I make this parameter work so every Item has custom parameter?
There's a point where a one-liner should stop being a one-liner and become a helper function...
1

You can use bracket notation

onClick={ () => this.props.remove(this.props[this.props.title]) }

2 Comments

@Vikast could you post your code where do you use this.props.remove? As I understood you have loop, could you post it?
@Vikast could you give feedback?

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.