gpt4 book ai didi

javascript - ReactJS - 刷新时 componentDidMount 中的 Prop 不立即可用

转载 作者:行者123 更新时间:2023-11-29 21:16:50 25 4
gpt4 key购买 nike

有一个令人沮丧的问题,希望有人能提供帮助。完整 repo 可在 https://github.com/georgecook92/Stir/blob/master/src/components/posts/viewPosts.jsx 获得.

直接进入代码 -

componentDidMount() {
const {user_id,token} = this.props.auth;
this.props.startLoading();
console.log('props auth', this.props.auth);
if (user_id) {
console.log('user_id didMount', user_id);
this.props.getUserPosts(user_id, token);
}

}

如果组件是通过 ui 从另一个组件加载的,它会按预期运行。但是,如果页面刷新,user_id 等不会立即提供给 componentDidMount。

我已经检查过它稍后可用,但我发现如果我将我的 AJAX 调用移动到渲染方法或其他生命周期方法(如 componentWillReceiveProps) - Prop 会不断更新并锁定 UI -不理想。

我也不确定如果我将 ajax 调用移至 render 方法,为什么每秒会进行多个 ajax 调用。

希望对您有所帮助!谢谢。

编辑。

    export function getUserPosts(user_id, token){
return function(dispatch) {

if (window.indexedDB) {
var db = new Dexie('Stir');
db.version(1).stores({
posts: '_id, title, user_id, text, offline',
users: 'user_id, email, firstName, lastName, token'
});

// Open the database
db.open().catch(function(error) {
alert('Uh oh : ' + error);
});

db.posts.toArray().then( (posts) => {
console.log('posts:', posts);
if (posts.length > 0) {
dispatch( {type: GET_POSTS, payload: posts} );
}
});
}

axios.get(`${ROOT_URL}/getPosts?user_id=${user_id}`, {
headers: {
authorisation: localStorage.getItem('token')
}
}).then( (response) => {
console.log('response from getPosts action ', response);

dispatch( {type: GET_POSTS, payload: response.data} );
dispatch(endLoading());

response.data.forEach( (post) => {

if (post.offline) {

if (window.indexedDB) {
db.posts.get(post._id).then( (result) => {
if (result) {
//console.log('Post is already in db', post.title);
} else {
//console.log('Post not in db', post.title);
//useful if a posts offline status has changed
db.posts.add({
_id: post._id,
title: post.title,
user_id: post.user_id,
text: post.text,
offline: post.offline
});
}
} )
}
}
} );

})
.catch( (err) => {
console.log('error from get posts action', err);
if (err.response.status === 503) {

dispatch(endLoading());
dispatch(authError('No internet connection, but you can view your offline posts! '));
} else {
dispatch(endLoading());
dispatch(authError(err.response.data.error));
}

});
}

}

最佳答案

我建议使用 componentWillReceiveProps 并将您的 Prop 保存为状态,以便在发生更改时触发重新渲染(例如, Prop 从未定义变为具有值)。这样的事情会起作用:

import React, {Component} from 'react';

// using lodash for checking equality of objects
import _isEqual from 'lodash/isEqual';

class MyComponent extends Component {
constructor(props={}) {
super();
this.state = props;
}

// when the component receives new props, like from an ajax request,
// check to see if its different from what we have. If it is then
// set state and re-render
componentWillReceiveProps(nextProps) {
if(!_isEqual(nextProps, this.state)){
this.setState(nextProps);
}
}
render() {
return (
<div>{this.state.ItemPassedAsProp}</div>
);
}
}

关于javascript - ReactJS - 刷新时 componentDidMount 中的 Prop 不立即可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39059488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com