简体   繁体   English

React:使用CSS类在数组中标记活动组件

[英]React: Tagging active component in an array with a CSS class

I am loading a series of sentences into an array Sentences in React. 我正在将一系列句子加载到React中的数组句子中。 On my front end I have one "active" sentence and after user form input the next sentence in the loaded array should become "active" with a new CSS class attached to it. 在我的前端,我有一个“活动”句子,在用户表单输入后,加载数组中的下一个句子应该变为“活动”,并附加一个新的CSS类。

How would I go about doing this? 我该怎么做呢? I understand SentenceList needs to keep track of the active_sentence and the Sentence needs to tell SentenceList has been updated, and when it does it should set the class "active" to the next sentence sequentially. 我理解SentenceList需要跟踪active_sentence并且Sentence需要告诉SentenceList已经更新,当它发生时它应该将类“active”设置为下一个句子。 But I'm not sure how to implement it. 但我不确定如何实现它。

SentenceList : SentenceList

var SentenceList = React.createClass({
  render: function() {
    var sentences = [];
    active_sentence = 0;

    //loop through sentences and push each sentence into array
    this.props.sentences.forEach(function(sentence) {

      // increment an index variable here and put an if index === active_sentence statement?

      //grabs @sentences from Rails
      sentences.push(<Sentence key={sentence.id} details={sentence} />)
    });

    return (
      <div>{sentences}</div>
    )
  }
});

Sentence : Sentence

var Sentence = React.createClass({
  getInitialState: function() {
    return {
      //
    }
  },

  addBlip: function(e) {
    var blipBody = this.refs.newBlip.getDOMNode().value;
    var sentenceId = this.props.details.id;
    var thisSentenceComponent = this;

    $.ajax({
      url: '/sentences/' + sentenceId + '/blips',
      type: 'POST',
      dataType: 'json',
      data: {blip: {body: blipBody}}
    });

    e.preventDefault();
  },

  render: function() {
    //get user input and submit blip and make next sentence "active"
    var phrase = this.props.details.body;
    var phrase_display = phrase.split("*");

    return (
      <div className="blipForm">
        {phrase_display[0]}
        {this.props.details.index}
        <form onSubmit={this.addBlip}>
          <input
            type="text"
            ref="newBlip"
          />
        </form>
        {phrase_display[1]}
      </div>
    )
  }
});

You can create a variable in state ( activeKey in this example) to keep track of which sentence key is considered active, then a prop can be passed to Sentence telling it whether it is active. 您可以在状态中创建变量(在此示例中为activeKey )以跟踪哪个句子键被认为是活动的,然后可以将prop传递给Sentence告诉它是否处于活动状态。 The function setActiveKey below can be used to update the active sentence when you load a new sentence : 下面的函数setActiveKey可用于在加载新句子时更新活动句子:

var SentenceList = React.createClass({
  getInitialState: function() {
    return {
      activeKey: false
    };
  },
  setActiveKey(newActiveKey){
    this.setState({activeKey: newActiveKey}); 
  },
  render: function() { 
    var sentences = [];
    this.props.sentences.forEach(function(sentence) {
      sentences.push(<Sentence isActive={this.state.activeKey === sentence.id} key={sentence.id} details={sentence} />)
    }.bind(this));
    return (
      <div>{sentences}</div>
    )}
});

Then within the render function of Sentence , you can the prop isActive , and if the value is true, you can render it with the active style: 然后在Sentence的渲染函数中,你可以使用prop isActive ,如果值为true,你可以使用活动样式渲染它:

render: function() {  
  var phrase = this.props.details.body
  var phrase_display = phrase.split("*");

  return (
    <div className="blipForm" style={this.props.isActive ? styles.active : styles.inactive}>
      {phrase_display[0]}
      {this.props.details.index}
      <form onSubmit={this.addBlip}>
        <input type="text"
             ref="newBlip" />
      </form>
      {phrase_display[1]}
    </div>
  )
}

And you can control your styles in a variable ( styles here): 您可以在变量中控制样式( styles在这里):

var styles={
  active:{
    //set active styles here
  },
  inactive:{
    //set inactive styles here
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM