I am confused a little bit about the idea of re-rendering.
First question: In
Reactwithoutreduxdo changing in componentspropstrigger re-render ?Second question: How to handle async data when using redux, in
react without reduxafterAJAXrequest i used to putsetStateinsidecomponentDidMount()so changing instatecauses component to re-render. Using redux i read that the only way you must useconnecthelper in order to make component re-render is that true?Third question: In the following code (Component that returns
navbarwith child elements that depends whether the user is authenticated or not) I triggered unexpected behavior sometimes thenavbarreturnsLoading...and stops forever. Other times it returnsLoading...at first then automatically changed toUser Profileif user logged in orSign Upif not. why is that happening?Fourth question: When using
reduxwhere should i put async data and make component waits these data before re-render?class Header extends Component{ renderComponent(){ switch(this.props.auth){ case null : return <li>Loading...</li>; case false: return <li><a>Log In</a></li>; default: return <li><a>User Profile<a/></li> } } render(){ return( <nav> <ul> {this.renderComponent()} </ul> </nav> ) } } const mapStateToProps = ({auth}) => { return { auth } } export default connect(mapStateToProps)(Header);
Action Creator
export const fetchUser = ()=>{
return async (dispatch) => {
const res = await axios.get('/api/currentuser');
dispatch({
type: FETCH_USER,
payload: res.data
});
}
};
Reducer
export default (state = null, action) => {
switch(action.type){
case 'FETCH_USER': return action.payload || false;
default:
return state;
}
}
Thank you, Any help would really be appreciated.