- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我使用 redux 的 native react 应用程序中,reducers 似乎工作正常,但 UI 不会根据状态修改进行更新。
这是我的 reducer :
const initialState = {
userId: 1,
userName: "nobody",
showIntroOnNextStart: true
}
export default function reducer(state = initialState, action) {
switch(action.type) {
case 'INCREMENT':
let newState = {
...state,
userId: state.userId + 1
};
console.log(newState);
return newState;
}
return state;
}
它将通过以下方式导出和导入:
import userProfile from './userProfile'
export { userProfile }
这是我的连接方法:
export default connect(state => ({
state: state.userProfile
}),
(dispatch) => ({
actions: bindActionCreators(actions, dispatch)
})
)(ApplicationContainer);
所以最终状态是这样的:
在我的应用程序组件的渲染方法中,我将状态传递给子组件:
render() {
const { state, actions } = this.props;
return ....lots of scenes...
<Scene key="home" component={HomeScreen} title="Home" state={state} {...actions} />
在特定的场景组件中,我使用这样的状态:
render(){
const { state, decrement, increment } = this.props;
return (
<View {...this.props} style={styles.container}>
<Text>{state.userId}</Text>
<Button onPress={increment}>Increment</Button>
</View>
);
}
增量操作按预期正常工作,每个 console.log-Output 都显示一个具有正确增量值的新状态对象。
但是 View 不会更新。在 reducer 函数中,状态按预期工作并在每次调用时递增 userId。但在渲染函数中,状态对象值从一开始就保持不变。
这里可能出了什么问题?
当我从 react-native-router-flux 中删除这个 Router-Stuff 时,一切正常。状态将是相同的实例并将被更新。但是一旦将组件封装到路由器的场景中,就不会发生状态更新
最佳答案
从我使用 Redux 的有限时间来看,不渲染的问题通常是对象的引用没有改变。如果你有一个深对象,你需要做深拷贝。我个人喜欢对 React 中的任何状态使用 immutable.js。
您可以尝试缩小问题范围的另一件事是在按增量后调用组件上的 this.forceUpdate()。如果它更新了 View ,那将使您更接近于找出问题所在。
关于javascript - react /归零 : modified state is not updated in view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37241194/
我有一个使用 PHP 设置页面输入字段值的表单,例如: // create and fill inputVarsArr with keys that have empty vals $troubleI
大家好,我是码农先森。 古话说的 "三十而立",正是担重之时,却大部分人在职场中都处于不上不下的尴尬境地。已经没有刚毕业时那股子冲劲,被生活和工作磨平了棱角。 在技术思想方面,
我想将这个“.wrapper wf”类归零。 (比如 header 和 .p)但是我不能成功,你能帮我弄清楚吗? here is the fiddle .wf{ -webkit-transfo
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Set all bytes of int to (unsigned char)0, guaranteed t
当使用STL容器时,我不确定默认分配器分配的一个int是否已经归零。以下代码对问题表示"is": #include #include int main() { using namespace
我有一个包含弱引用的对象的以下简单代码: //界面 @interface GMWeakRefObj : NSObject @property (weak) id object; @end //执行 @
我是 React/redux 的新手。我有一个用于身份验证的 Redux 操作,之后我需要重定向到一个确认页面主页。我不知道如何重定向 这是我的 index.js import React, { Pr
我有一个 vector我想把它归零。我需要尺寸保持不变。 通常的方法是遍历所有元素并重置它们。但是,vector是 specially optimized容器,根据实现,每个元素可能只存储一位。有没有
我错误地将密码存储为 Java 程序中的字符串。我想防止此字符串密码出现在堆转储/内存转储中。理想情况下,我应该使用 char[] 并在使用后将其填充为零(如本文 - https://nvisium.
是否 x.resize(1024)保证valarray x会归零吗? 是否std::valarray z(1024);保证它也将被填充为零? 对于 Mac、Windows 和 Linux 是这样吗?即
下面的 java 代码是否足以清除内存中的 key (将其所有字节值设置为 0)? zerorize(SecretKey key) { byte[] rawKey = key.getEncod
我是 React 和 Redux 的初学者,我正在尝试设置一个非常简单的登录表单和重定向。我稍后会添加 react-router 或 react-router-redux。 我真的不明白我必须在哪里放
最近我一直在使用 cgo 在我的一个项目中设置 libsodium,以便使用 crypto_pwhash_str 和 crypto_pwhash_str_verify 函数. 这一切都进行得非常顺利,
我正在使用以下代码从网站检索一些数据。 Public Function giveMeValue(ByVal link As String) As String Set htm = CreateO
我想要获得与在 Photoshop 中关闭其中一个 channel 相同的结果。我正打算尝试循环改变颜色的每个像素。有更好的方法吗? 最佳答案 使用 Core Image 的颜色矩阵滤镜。 The a
我有以下用于导航的组件: import React, { Component } from "react"; import { connect } from 'react-redux'; import
试图在 Redux-React-API 丛林的黑暗深处找到方向 - 设法从 API 和 console.log 中获取数据 - 但我和我的 Google 技能都无法找出它不呈现的原因。 react 组
在我使用 redux 的 native react 应用程序中,reducers 似乎工作正常,但 UI 不会根据状态修改进行更新。 这是我的 reducer : const initialState
这tutorial Dan Abramov 提出,使用作用于全局状态(而不是一部分状态)的选择器的优势在于它们允许容器与状态结构的知识分离。 如果是这样的话,我们不应该也避免直接将状态值映射到 Pro
背景 我正在构建一个包装程序包,它组合了定义的命令并允许它们在 cli 或交互式 shell 上下文中执行。命令在类似这样的结构中定义(显示相关字段): type Handler func(c *Co
我是一名优秀的程序员,十分优秀!