React Native计算this.state中的表达式

时间:2016-05-17 22:34:19

标签: reactjs react-native

一直试图让这样的事情发挥作用。 React Native的新手,所以我不确定它是否被允许。

class SearchPage extends Component {
  constructor(props) {
    super(props);
    this.state = {
      day: '',
      time: ''
   };

  _renderContent(section, i, isActive) {
    var name = section.identifier;
    // for example the value of name is 'day'
    // how would I be able to access this.state.day here? 
  }

  // other code including render function
}

我也试过this.state.{name}等其他事情,但他们不能工作。有人可以帮帮我吗?谢谢!

1 个答案:

答案 0 :(得分:2)

state将是一个javascript对象,因此您可以评估类似这样的内容:

const variableName = "day"

this.state[variableName] // this is equivilant to this.state.day