Я хочу иметь возможность выводить ключ и значение ключа элементов в моем состоянии. Я пытался использовать {[this.state[field]]}
, но это тоже не сработало.
Пример:
https://jsfiddle.net/n5u2wwjg/164470/
class App extends React.Component {
constructor(props) {
super(props)
this.state = {
type: 'valueOfType',
subType: 'valueOfSubType',
anotherThing: 'valueOfOther'
}
}
renderItem = (field) => {
return <div>{['nameOfKey']}: {field}</div>
}
render() {
const { type, subType, anotherThing } = this.state;
return (
<div>
<p><strong>Actual output:</strong></p>
{this.renderItem(type)}
{this.renderItem(subType)}
{this.renderItem(anotherThing)}
<hr/>
<p><strong>Desired output:</strong></p>
<div>type: valueOfType</div>
<div>subType: valueOfSubType</div>
<div>anotherThing: valueOfOther</div>
</div>
)
}
}