Redux商店没有';更新组件道具
我试图从我的商店调用一个API来更新组件的状态,这里得到加密的价格 作为回报,我使用了我状态的克隆(这里是nextState),nextState的日志中很好地填充了商品价格,但我的组件只获得initialState 代码如下: 我的组件Redux商店没有';更新组件道具,redux,react-native-android,Redux,React Native Android,我试图从我的商店调用一个API来更新组件的状态,这里得到加密的价格 作为回报,我使用了我状态的克隆(这里是nextState),nextState的日志中很好地填充了商品价格,但我的组件只获得initialState 代码如下: 我的组件 import React from 'react'; import { StyleSheet, Text, View, Button, ImageBackground,TouchableOpacity, Image } from 'react-native';
import React from 'react';
import { StyleSheet, Text, View, Button, ImageBackground,TouchableOpacity, Image } from 'react-native';
import {widthPercentageToDP as wp, heightPercentageToDP as hp} from 'react-native-responsive-screen';
import { connect } from 'react-redux'
class Bitcoin extends React.Component {
constructor(props){
super(props);
this.state = {
}
}
componentDidMount() {
const action = { type: 'PRICES', value: this.state.cryptos}
this.props.dispatch(action)
console.log(this.props.cryptos)
}
componentDidUpdate() {
console.log("Component did Update : ")
console.log(this.props.cryptos)
}
render() {
return (
<View>
<Text style={styles.title}>Bitcoin !</Text>
<Text> {this.props.cryptos[0].price} </Text>
</View>
)
}
}
const styles = StyleSheet.create({
title: {
marginTop: wp("10%")
},
});
const mapStateToProps = (state) => {
return {
cryptos: state.Crypto.cryptos
}
}
const mapDispatchToProps = (dispatch) => {
return {
dispatch: (action) => { dispatch(action) }
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Bitcoin)
欢迎来到StackOverflow。
我想你对Redux工作流还不熟悉。就是这样
动作描述一个动作。reducer接收操作并指定存储的更改方式
操作必须是纯javascript对象。和减速器功能必须是
此处禁止在减速器内进行以下操作:
Mutate its arguments;
Perform side effects like API calls and routing transitions;
Call non-pure functions, e.g. Date.now() or Math.random().
在您的示例中,通过调用fetch。您正在进行API调用
我邀请您阅读本指南以了解更多信息:如何在redux应用程序中引入API调用和异步。()欢迎来到StackOverflow。
我想你对Redux工作流还不熟悉。就是这样
动作描述一个动作。reducer接收操作并指定存储的更改方式
操作必须是纯javascript对象。和减速器功能必须是
此处禁止在减速器内进行以下操作:
Mutate its arguments;
Perform side effects like API calls and routing transitions;
Call non-pure functions, e.g. Date.now() or Math.random().
在您的示例中,通过调用fetch。您正在进行API调用
我邀请您阅读本指南以了解更多信息:如何在redux应用程序中引入API调用和异步。()您当前的方法不利于redux。减速器函数必须是纯函数。这里不允许有副作用。您应该使用thunk中间件将与网络相关的代码放到组件本身或动作创建者。您当前的方法不利于redux。减速器函数必须是纯函数。这里不允许有副作用。您应该使用thunk中间件将网络相关代码放置到组件本身或操作创建者。