zavtra zavtra - 5 months ago 10
JSON Question

Interpolate JSON in React Component's attribute?

I have a React component whose value I would like to set via JSON in the attribute. However, I'd like concatenate a letter or word, but neither show up. Is there a proper way to concatenate strings inside of a Component's attribute? Here, I'm setting the

detail
attribute of a TableView cell.

<Cell cellstyle="RightDetail" accessory="DisclosureIndicator"
title="Height"
detail={this.state.height} onPress={this.navHeight.bind(this)}/>


Which produces this:
175
(the value set for height).

I'd like to find a way to do something like:

detail="\{this.state.height} meters"


Is this possible to do in React/ES6?

Answer

You can use the backtick notation if you're in ES6,

detail={`${this.state.height} meters`}

Per here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals

Comments