program_bumble_bee
program_bumble_bee

Reputation: 305

Accessing axios body parameter outside the URL

I have this axios request:

 axios.all([

    axios.post(baseURL+'sentiments', {
      sentiment_type: 'positive',
      filter_tab: type
    }),

    axios.post(baseURL+'sentiments', {
      sentiment_type: 'negative',
      filter_tab: type
    })
])
    .then(axios.spread(result1, result2) => { 
          console.log(result1),
          console.log(result2)
    })
    .catch(err => {
          console.log(err)
    })

Then in my render method, What I am trying to achieve is to access 'sentiment_type' value 'positive' into some variable or something, to further use it for conditional rendering.

if(sentiment_type === positive){ 
   then do this
 }
else{ 
      do something else
    }

Is there any way to do this? Please guide.

Upvotes: 0

Views: 160

Answers (1)

M1sterPl0w
M1sterPl0w

Reputation: 203

I assume this part of code, is part of a component. You could store these variables in a local store. Here is a link for more information: State and Lifecycle

and an example, I hope this will help you.

    class parentComponent extends React.Component {
  state = {
    sentiment_types: [];
  }

  componentDidMount() {
    axios.post(baseURL+'sentiments', {
              sentiment_type: 'positive',
              filter_tab: type
    })
    .then((firstResponse) => { 
            axios.post(baseURL+'sentiments', {
                sentiment_type: 'negative',
                  filter_tab: type
            })
            .then((secondResponse) => {
                // Here you can store you results into the state
                this.setState({
                    sentiment_types: ['positive', 'negative'];
                })
            })
            .catch(err => {
                console.log(err);
            })          
          console.log(result)
    })
    .catch(err => {
          console.log(err)
    })
  }

  render() {
   <childComponent sentiment_types={this.state.sentiment_types} />
  }
}

const childComponent = (props) => {
    if(props.state.sentiment_types.includes('positive')){
        return <h1>oke</h1>
    }
}

Upvotes: 2

Related Questions