- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 React Redux 新手。我想在用户按下搜索按钮时添加加载文本,并在数据返回或操作完成时关闭文本。在普通的异步函数中,我可以在回调之前和之后切换 isLoading 标志。
但是,在我的应用程序中,我正在调度一个返回“类型”和“有效负载”的操作,这是一个 promise 。然后,中间件 redux-promise 会“自动”转换该 Promise 有效负载并将其发送到 reducer 。换句话说,中间件在幕后为 Promise 执行 .then 操作,并为我的 reducer 提供正确的数据。
在这种情况下,我不确定如何向 View 添加加载文本,因为一旦我调用 this.props.getIdByName(this.state.value),我就不知道数据何时返回。对我来说,合理的位置是在 reducer 内部,因为那是数据返回的时候。但是,我认为这将是一个糟糕的方法,因为 reducer 不应该执行这样的任务?
在我的组件中,我的提交具有以下功能
handleSubmit(e) {
e.preventDefault();
this.props.getIdByName(this.state.value);
}
在我的 actions/index.js 文件中,我有以下操作生成器
export function getIdByName(name) {
const FIELD = '/characters'
let encodedName = encodeURIComponent(name.trim());
let searchUrl = ROOT_URL + FIELD + '?ts=' + TS + '&apikey=' + PUBLIC_KEY + '&hash=' + HASH + '&name=' + encodedName;
const request = axios.get(searchUrl)
return {
type: GET_ID_BY_NAME,
payload: request
}
}
在我的 reducer /reducers.jsx 中
export default function (state = INITIAL_STATE, action) {
switch (action.type) {
case GET_ID_BY_NAME:
console.log(action.payload.data.data.results[0].id); <-- here the data comes back correctly because reduer called the promise and gave back the data for me
return {...state, id: action.payload.data.data.results[0].id};
default:
return state;
}
}
在我的主 index.js 文件中,我使用以下中间件创建了商店
const createStoreWithMiddleware = applyMiddleware(
promise,
thunk
)(createStore);
最佳答案
当您在使用 redux-promise
时分派(dispatch)以 Promise
作为负载的操作时,redux-promise
中间件将捕获该操作,等待 Promise
解析或拒绝,然后重新分派(dispatch)该操作,现在将 Promise
的结果作为其负载。这意味着您仍然可以处理您的操作,并且一旦您处理它,您就知道它已经完成。所以你是对的, reducer 是处理这个问题的正确地方。
但是,您也认为 reducer 不应该有副作用。他们应该只建立新的国家。那么,答案就是通过在 Redux 中更改应用程序的状态来显示和隐藏加载指示器:)
我们可以向 Redux 存储添加一个名为 isLoading
的标志:
const reducers = {
isLoading(state = false, action) {
switch (action.type) {
case 'START_LOADING':
return true;
case 'GET_ID_BY_NAME':
return false;
default:
return state;
}
},
// ... add the reducers for the rest of your state here
}
export default combineReducers(reducers);
每当您要调用 getIdByName
时,请在调用之前分派(dispatch)类型为 'START_LOADING'
的操作。当 getIdByName
被 redux-promise
重新分派(dispatch)时,您会知道加载已完成,并且 isLoading
标志将被设置回 false。
现在我们有一个标志来指示我们是否正在 Redux 存储中加载数据。使用该标志,您可以有条件地渲染加载指示器。 :)
关于reactjs - 使用 Promise 中间件为 React-Redux 应用程序添加加载指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43661567/
有人可以给我一个更简单的以下代码的解决方案(它正在展开给定结构 0xFC :: len :: payload :: ... :: 0x0A :: 0x0D 的整数列表): object Payload
我已经在我的网站上安装了 SSL 证书,但 intermediate.crt 无法正常工作。任何 SSL 检查器(例如 GeoTrust Checker)都告诉我,缺少中间 key 。网站上已经使用了
如何让图像从这个框的中间开始? (中间纵横) 最佳答案 有几种方法可以做到这一点,如果它需要在所有浏览器(IE7+ 和其他浏览器)中工作,你需要做不同的事情来让它在某些情况下工作。 使用绝对位置
如何强制 min-height 和 vertical-align:middle 为 td 元素或其内部元素工作? 最佳答案 td 元素上的 height 等同于 min-height,因为如果需要,表
我正在尝试自动滚动到订单簿的中间行。 我有 orderBook div,其中放置了带有 orderBook 的表。该表的其中一行有一个 id middleRow。我想做的是滚动该行并将其放置在 ord
我正在尝试在 javascript 中计算绝对定位元素的 transform-origin 属性,以便它们在悬停时填充整个视口(viewport)。 我尝试通过 x 除以窗口宽度和 y 除以窗口高度来
我有休闲字符串 ' this is my string ' 是否可以删除开头和结尾的所有空格,只在单词之间留一个空格。 要选择我使用过的所有空间: SELECT regexp_replace('
我正在设法创建我的第一个复杂的 J2E 解决方案,并且在每个教程中我都发现了某种中间表的用法,如下所示: 表:用户、用户角色、角色虽然逻辑会简单地向用户表添加一个键来引用它在角色表上的角色,但为什么要
我正在寻找以下解决方案。我想定位一个图像元素,例如 在中间。所以高度是视口(viewport)的高度,宽度会自动设置,图像的中间应该在视口(viewport)宽度的中间。 我搜索的一个例子就像下面的网
我正在设计一种布局,它更像是注册用户的个人仪表板。我让它变得简单,使用基本的 2 列网格,一个用于侧边栏,一个用于主要内容。 因为,例如,80% 的网站使用将发生在一个单独的子系统中,在无 chrom
我有三个不同的 div 标签(不在彼此内部)和代码,所以它有一个把单词放在左边、中间或右边,但中心非常偏离中心。这是 HTML 代码: .desc { float: right; color:
我有以下CSS http://jsbin.com/azivip/75/edit我想让黄色的 div 高度填充蓝色和绿色 div 之间的空间。使用高度继承似乎使 div 超出了绿色 div。 有什么想法
我不得不在其父元素的中间放置一些文本。我用下面的代码实现了它: #div1 { position: relative; margin: 0; padding: 0; } #div2 {
发现一个使用合法证书(由thawte 签名)的网站,但所有浏览器都会拒绝它。我不明白为什么。thawte 的支持告诉我一个域有两个证书,然后将这个 https://www.sslshopper[dot
我正在尝试使用 OpenSSL 创建证书链,但出于某种原因,当我在我的计算机上安装我的根 CA 并尝试验证证书链时,它总是告诉我它找不到证书的颁发者.为了让事情发生,我必须安装中间 CA,这是没有意义
我看到 REST 的一大好处是依赖 HTTP 缓存。我不是在争论这个,而是完全认同这个想法。但是,我从来没有看到对中间 HTTP 缓存的更深入的解释。 如果我将 Cache-control heade
查看此图片 Facebook Messenger Android App Buttons ( MESSENGER\ACTIVE ) 我怎样才能做到这一点? 详细信息:- 带有 2px 红色边框的 di
我的任务是制作漂亮的文本,在文本中间加一条白线,如下图所示。是否可以使用 css 来实现?这是 Fiddle .container{ height:200px; width:400px;
在拉丁文字中,字母有大写和小写形式。在 Python 中,如果你想比较两个字符串而不考虑它们的大小写,你可以使用 'string'.upper() 或 'string'.lower() 将它们转换为相
我正在使用 awk 对文件进行一些文本处理。例如删除尾随空格。 awk '{gsub(/ +$/, "")} {print $0}' filename 这很好用。但是当我将输出重定向到原始文件时。它变
我是一名优秀的程序员,十分优秀!