Skip to content Skip to sidebar Skip to footer

Do I Need Componentwillreceiveprops And Replaceprops When Using Reactjs + Redux?

i'm learning redux along side with react and did a first app to catch a few infos from Destiny and present for the user. The app has a select box where the user can choose one of t

Solution 1:

So the quick answer is no, it's not necessary. Why ? Well, you're not really using redux yet. If you look at that ajax call your are doing in replace props, getAjax, I inspected that in your codebase, and see you're calling setState in the component after receiving a request there.

With redux, you would rather use an action and reducer. The action would be handled, calling the api, and setting the state in the redux "store" with a reducer after receiving this data.

Ok so a full blown example would be something like the following, just first add in redux-thunk, it will definitely help you out going forward, be sure to go read through the example on the README to get a better idea of the how and why.

functionstartLoading() {
  return {
    type: 'LOADING_STARTED',
    isLoading: true
  }
}

functiondoneLoading(){
  return {
    type: 'LOADING_ENDED',
    isLoading: false
  }
}

functionsetActivity(result) {
  let lastGist = result[0];
  let activity = {
    identifier: result.display.identifier,
    title: (result.display.hasOwnProperty('advisorTypeCategory'))? result.display.advisorTypeCategory : '',
    name: (result.hasOwnProperty('details') && result.details.hasOwnProperty('activityName')) ? result.details.activityName : '',
    desc: (result.hasOwnProperty('details') && result.details.hasOwnProperty('activityDescription')) ? result.details.activityDescription : '',
    backgroundImg: (result.display.hasOwnProperty('image')) ? 'http://bungie.net' + result.display.image : '',
    modifiers: (result.hasOwnProperty('extended') && result.extended.hasOwnProperty('skullCategories')) ? result.extended.skullCategories : [],
    bosses: (result.hasOwnProperty('bosses')) ? result.bosses : [],
    items: (result.hasOwnProperty('items') && result.display.identifier == "xur") ? result.items : [],
    bounties: (result.hasOwnProperty('bounties')) ? result.bounties : []
  }
  return {
    type: 'SET_ACTIVITY',
    activity: activity
  }
}

exportfunctionfindActivity(activity_id) {
 returndispatch => {
   dispatch(startLoading())
   $.get(activity_id, (result)=>{
     dispatch(doneLoading())
     if(response.status == 200){
       dispatch(setActivity(response.json))
     }else { 
       dispatch(errorHere)
     }
   })
 }
}

So it might look a bit intimidating at first, but after a go or two, it will feel more natural doing things this way, instead of in the component.

Solution 2:

There shouldn't be any need for replaceProps, as the props will be updated automatically. componentWillReceiveProps is a chance for you to take a peek at what is to come in this lifecycle.

Note: You should never clobber this.props as that is used internally.

I would recommend comparing this.props to nextProps inside componentWillReceiveProps to see if the selected Activity has changed. If so, then fire the ajax call (which I recommend using a redux action passed into the component).

Solution 3:

Yeah, I screwed up the comment haha sorry, on the SelectContainer.jsx now I'm doing that to retrieve the activity json after the select change:

constmapDispatchToProps = (dispatch) => {
    return {
        onSelectChange: (activity) =>{
            dispatch(changeApiUrl(activity));
            dispatch(findActivity(activity));
        }
    }
}

UPDATE

import { connect } from'react-redux';
import { changeApiUrl, findActivity } from'../actions/index.jsx';
importActivityComponentfrom'../components/ActivityComponent.jsx';

constmapStateToProps = (state) => {
    return state.activity;
}

exportclassActivityContainerextendsActivityComponent {
    componentDidMount() {
        const { dispatch, identifier } = this.props;
        dispatch(findActivity(identifier));
    }
}

exportdefaultconnect(mapStateToProps)(ActivityContainer);

Solution 4:

Generally speaking on life cycle of methods in react with redux. you should use redux methods. unless you have to use in react life cycle methods.

Post a Comment for "Do I Need Componentwillreceiveprops And Replaceprops When Using Reactjs + Redux?"