I am confused a little bit about the idea of re-rendering.
First question: In
React
withoutredux
do changing in componentsprops
trigger re-render ?Second question: How to handle async data when using redux, in
react without redux
afterAJAX
request i used to putsetState
insidecomponentDidMount()
so changing instate
causes component to re-render. Using redux i read that the only way you must useconnect
helper in order to make component re-render is that true?Third question: In the following code (Component that returns
navbar
with child elements that depends whether the user is authenticated or not) I triggered unexpected behavior sometimes thenavbar
returnsLoading...
and stops forever. Other times it returnsLoading...
at first then automatically changed toUser Profile
if user logged in orSign Up
if not. why is that happening?Fourth question: When using
redux
where 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.