Matus
Matus

Reputation: 100

Changing/Processing data after async fetch in componentDidMount

I'm trying to change data after fetching them in componentDidMount function.

Let's say I'm fetching one object:

{
  text: "hello world"
}

​but I want to add "!" to this string after fetching in componentDidMount. My question si, where should I edit fetched data? When I'm trying to edit it in componentDidMount I always get error saying that I cannot edit 'undefined'.

Thanks in advance.

Upvotes: 0

Views: 49

Answers (1)

Robsonsjre
Robsonsjre

Reputation: 1606

Be a little more specific, its better if you show your fetch function and your modify function also. But, I can guess something similar to it:

 componentDidMount() {
    fetch(reqObj)
      .then(json => {
        const modifyJson = JSON.stringify(json) + '!'
        this.setState({text: modifyJson})
      })
  }

Upvotes: 1

Related Questions