0

I would like to push value in topicsVisited array if this condition is true if (totalPagesInSelectedTopic == this.state.topicPageNo + 1) { and assign it value to the state. However in the console.log(this.state.topicsVisited); its returning empty array.

import {React, ReactDOM} from '../../../../build/react';

import SelectedTopicPageMarkup from './selected-topic-page-markup.jsx';
import NextPrevBtn from './next-prev-btn.jsx';

export default class SelectedTopicPage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      topicPageNo: 0,
      total_selected_topic_pages: 1,
      topicsVisited: []
    };
  };
  navigateBack(topicPageNo) {
    if (this.state.topicPageNo > 0) {
      topicPageNo = this.state.topicPageNo - 1;
    } else {
      topicPageNo = 0;
    }
    this.setState({topicPageNo: topicPageNo});
  };
  navigateNext(totalPagesInSelectedTopic) {
    let topicPageNo;
    if (totalPagesInSelectedTopic > this.state.topicPageNo + 1) {
      topicPageNo = this.state.topicPageNo + 1;
    } else if (totalPagesInSelectedTopic == this.state.topicPageNo + 1) {
      this.props.setTopicClicked(false);

      let topicsVisited = this.state.topicsVisited;
      topicsVisited.push(this.props.topicsID);
      this.setState({topicsVisited: topicsVisited});
    } else {
      topicPageNo = this.state.topicPageNo;
    }
    this.setState({topicPageNo: topicPageNo});
  };
  render() {
    let topicsID = this.props.topicsID;
    let topicPageNo = this.state.topicPageNo;
    console.log(this.state.topicsVisited);
    return (
      <div>
        {this.props.topicPageData.filter(function(topicPage) {
          // if condition is true, item is not filtered out
          return topicPage.topic_no === topicsID;
        }).map(function(topicPage) {
          let totalPagesInSelectedTopic = topicPage.topic_pages.length;
          return (
            <div>
              <div>
                <SelectedTopicPageMarkup headline={topicPage.topic_pages[0].headline} key={topicPage.topic_no}>
                  {topicPage.topic_pages[topicPageNo].description}
                </SelectedTopicPageMarkup>
              </div>
              <div>
                <NextPrevBtn moveNext={this.navigateNext.bind(this, totalPagesInSelectedTopic)} key={topicPage.topic_no} moveBack={this.navigateBack.bind(this, topicPageNo)}/>
              </div>
            </div>
          );
        }.bind(this))}
      </div>
    );
  };
};
0

1 Answer 1

1

I've created a simple exmaple for you.

increment(){
    let newCount = this.state.count + 1,
        right = this.state.rightElements,
        left = this.state.leftElements;
    newCount % 2 === 0 ? right.push(newCount) : left.push(newCount)

    this.setState({
      count: newCount,
      rightElements: right,
      leftElements: left,
    });
  }

The whole worked example you can find here

Thanks.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I had actually tried it with simple example earlier and it worked properly. However, for some reason it not working in my case.
@RahulDagli could you create fiddle example?
I've been able to figure out the root cause. Since i'm unmounting SelectedTopicPage component from its parent component, when it reaches end of that topic page the state resets to its initial state.

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.