在componentWillReceiveProps中调度时的无限循环

haz*_*ah0 13 reactjs react-router redux react-redux react-router-redux

我有一个由react-router(path ="profile /:username")加载的Profile组件,组件本身如下所示:

...
import { fetchUser } from '../actions/user';

class Profile extends Component {
  constructor(props) {
    super(props);
  }
  componentDidMount() {
    const { username } = this.props;
    this.fetchUser(username);
  }
  componentWillReceiveProps(nextProps) {
    const { username } = nextProps.params;
    this.fetchUser(username);
  }
  fetchUser(username) {
    const { dispatch } = this.props;
    dispatch(fetchUser(username));
  }
  render() {...}
}

export default connect((state, ownProps) => {
  return {
    username: ownProps.params.username,
    isAuthenticated: state.auth.isAuthenticated
  };
})(Profile);
Run Code Online (Sandbox Code Playgroud)

而fetchUser动作看起来像这样(redux-api-middleware):

function fetchUser(id) {
  let token = localStorage.getItem('jwt');
  return {
    [CALL_API]: {
      endpoint: `http://localhost:3000/api/users/${id}`,
      method: 'GET',
      headers: { 'x-access-token': token },
      types: [FETCH_USER_REQUEST, FETCH_USER_SUCCESS, FETCH_USER_FAILURE]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我添加componentWillReceiveProps函数的原因是当URL更改为另一个时响应:username并加载该用户配置文件信息.乍一看,一切似乎都有效,但后来我注意到调试时,在无限循环中调用了componentWillReceiveProps函数,我不知道为什么.如果我删除componentWillReceiveProps,那么配置文件不会使用新用户名更新,但我没有循环问题.有任何想法吗?

Avi*_*ash 17

尝试添加条件来比较道具.如果您的组件需要它.

componentWillRecieveProps(nextProps){
 if(nextProps.value !== this.props.value)
  dispatch(action()) //do dispatch here 
}
Run Code Online (Sandbox Code Playgroud)


Ken*_*ong 10

componentWillReceiveProps处于无限循环中,因为调用fetchUser将调度将更新道具的操作.

添加比较以在分派操作之前检查特定支柱是否发生更改. 编辑:

React 16.3+ componentWillReceiveProps中将慢慢弃用.

这是推荐使用componentDidUpdate的地方componentWillReceiveProps

componentDidUpdate(prevProps) {
  if (this.props.params.username !== prevProps.params.username) {
    dispatch(fetchUser(username));
  }
}
Run Code Online (Sandbox Code Playgroud)

请参阅https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#fetching-external-data-when-props-change


小智 5

如果你有一些路径参数如profile /:username,你可以简单地比较一下props.location.pathname

componentWillReceiveProps(nextProps){    
    if(nextProps.location.pathname !== this.props.location.pathname){
          dispatch()
        }
 }  
Run Code Online (Sandbox Code Playgroud)