- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有一个令人沮丧的问题,希望有人能提供帮助。完整 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/
我仍在学习如何将 API 数据与 react 和 nextjs 一起使用。但是,为什么我的函数只在我编写 {props.props.title} 而不是我期望的 {props.title} 时起作用?
我仍在学习如何将 API 数据与 react 和 nextjs 一起使用。但是,为什么我的函数只在我编写 {props.props.title} 而不是我期望的 {props.title} 时起作用?
我正在用 TypeScript 构建一个 React 应用程序。我有一个 RequiresPermission基于谓词的组件应该渲染一个或另一个组件并转发所有 Prop 。 type Props =
我想通过 gatsby 布局传递我所有的 props。例如: import React, { Component } from 'react'; export default class Exampl
如果我使用合成属性,那我为什么不直接说: self.property = nil; 这将释放引用计数,并确保我没有悬挂指针。 看起来很简单,但我看到的 99% 的代码似乎都是这样做的: [proper
Eslint 抛出 eslint(react/prop-types) 错误,尽管已经声明了 propTypes。我正在使用 eslint-plugin-react 我研究了其他几个类似的问题以及 li
我正在使用以下由 linter eslint-plugin-react 解析的代码。它返回警告: "product is missing in props validation" 当我在底部的 pro
我正在尝试在 React 应用程序中添加 TypeScript。 版本: "react": "16.9.0", "typescript": "3.5.3", 我有一个像这样的数组 import aLo
我有一个组件 . 如果组件没有 this.props.children , 我想设置 Prop ariaLabel作为isRequired ,否则 in 可以是可选的。我该怎么做? ariaLabe
我应该用一个代替另一个吗?一起使用它们更好吗?谢谢。 最佳答案 prop in obj 检查 obj 是否有名为 prop 的属性,即使它只是从原型(prototype)继承而来。 obj.hasOw
我的组件 Text有 2 个 Prop :isHideable: boolean和 hidden: boolean .我如何允许 Hidden仅在 isHideable 时作为 Prop 是true
我试图将带有一些 Prop 的功能组件发送到另一个组件,并在接收组件中尝试键入检查该组件的某些 Prop 。这是代码: // BaseButton.tsx export type ButtonProp
是否可以从也作为 prop 传递的未知组件推断出正确的 props 类型? 如果已知组件(存在于当前文件中),我可以获得 Prop : type ButtonProps = React.Compone
我对 react 还很陌生,这是我正在努力解决的问题。 有一个父组件 家长 它将 Prop 传递给 child 。 其中一个 Prop ,包括一个要渲染的元素,如下所示: 在子组件中,我想获取这个组
我想做一个 Tabs推断 active 的可能值的组件prop 基于它的 child 拥有的东西 name Prop 。这就是我尝试这样做的方式: import React from 'react'
我对 react 还很陌生,并且只有当用户开始向下滚动时,我才尝试将更多信息加载到记录数组中。问题是新信息出现在数组中,但如果您尝试调用它,它将返回undefined。我在这里不明白什么? 父组件:
因此,如果我对一个组件有很多不同的 Prop ,我希望我可以做类似的事情 const { ...props } = props; 而不是 const { prop1, prop2, prop3, ..
这是我寻求指导的问题类型,因为我不确定我正在寻找的内容是否存在...... 上下文:我正在使用 Firestore 来保存数据,并且正在构建一个可重用的自定义 Hook (React 16.8),以便
我有一个 React 组件,它获取一个配置对象作为 prop,它看起来像这样: { active: true, foo: { bar: 'baz' } } 在
如何附加另一个属性?我有一个 react 组件,其中有人传入 ...props,我想附加一个额外的 Prop 最佳答案 请记住,传递 Prop 的顺序将决定将哪个值传递给该组件。这适用于有两个同名
我是一名优秀的程序员,十分优秀!