O
O
Olga2018-05-14 14:38:05
React
Olga, 2018-05-14 14:38:05

How to work with data from query?

There is a FAQ page, respectively, there is a list of questions, by clicking on the question the answer should open. Previously, it was done like this, there was a function that, when clicked, passed the index of the question on which the click was made, was shown in the answer. if there was a second click on the same question, then the question index was removed from the state and the answer was hidden.

class Questions extends PureComponent {
constructor(...args) {
    super(...args);
    this.handleOnSelectQuestion = ::this.handleOnSelectQuestion;
                this.state = {
      selectQuestion: [],
}}
handleOnSelectQuestion(question) {
this.setState(({selectQuestion}) => {
      if(selectQuestion.indexOf(question) > -1){
        return {
          selectQuestion: selectQuestion.filter(questionId => questionId !== question)
        };
      }
      return {
        selectQuestion: [... selectQuestion, question]
      };
    });
}
}

render() {
return(
<div className={styles.question_container}>
              {questionsList.map((question, index) => (
                <QuestionsCard
                  key={index}
                  title={question.question}
                  answer={question.answer}
                  onSelect={partial(this.handleOnSelectQuestion, index)}
                  isOpen={selectQuestion.indexOf(index) > -1}
                  index={index}
                />
                )
              )}
            </div>
)
}

now this option is not suitable, you need to implement the same thing using location.query. I can't figure out how to save the question index in the query array when clicking on a question. While I sketched something like this
const {location} = this.props;
const query = {...location.query, question: [...location.query.question, question]};

but in the console the indexes are for some reason both lowercase and numeric. console.log(query.question) => take.ms/23zdM

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-05-14
@melkaya94

The utility you use to parse query string returns values ​​as strings. Which is obvious.
What is the question, exactly? Don't know how to cast string to number ?
It is possible like this:

const string = '1';
const number = Number.parseInt(string);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question