2

I'd like to set state of my React component from query values.

componentDidMount() {
    const query = queryString.parse(this.props.location.search);
    this.setState(query)
  }

It works well, but some fileds of state should be integer type. But queryString.parse returns objects which contains only string values. Is there any simple way to convert it? There is Trafaret library in Python, does JS have smth like that?

P.S. I want to use it for paginate, for example. Query params should contains offset, limit, number page etc.

7
  • parseInt() is what you are looking for: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Apr 26, 2019 at 19:26
  • Yes I know about it of course. But I asked about some schema to parse. Like in Python's trafaret. Commented Apr 26, 2019 at 19:32
  • What about JSON.parse(JSON.stringify(obj)) Commented Apr 26, 2019 at 19:34
  • @Neil, no it does not work :( Commented Apr 26, 2019 at 19:39
  • what does this.props.location.search look like? Commented Apr 26, 2019 at 19:43

1 Answer 1

2

Use the library query-string: https://github.com/sindresorhus/query-string

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

Comments

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.