【发布时间】:2019-04-18 04:11:32
【问题描述】:
我正在为我的多语言应用程序和 MySQL 使用 react-localize-redux 来获取数据。我的一项操作需要语言环境数据将其作为参数传递给后端,以便后端以正确的数据进行响应。但是当区域设置可用时,操作被调用并且应用程序崩溃,我该如何解决这个问题? 这是代码:
import React, { Component } from 'react'
import RestaurantCard from './RestaurantCard';
import {Row} from 'react-bootstrap';
import { connect } from 'react-redux';
import {getAllRestaurants} from "../actions/restaurantActions";
import { withLocalize } from 'react-localize-redux';
class RestaurantCardColumns extends Component {
constructor(props){
super(props);
}
componentDidMount(){
this.props.getAllRestaurants(this.props.activeLanguage);
}
render() {
if(this.props.loading || this.props.restaurants === null){
return <p>Loading...</p>
} else {
return (
<Row>
<RestaurantCard data = {this.props.restaurants[0]}/>
<RestaurantCard data = {this.props.restaurants[1]}/>
<RestaurantCard data = {this.props.restaurants[2]}/>
<RestaurantCard data = {this.props.restaurants[3]}/>
</Row>)
}
}
}
const mapStateToProps = (state) =>{
return {
auth: state.auth,
errors: state.errors,
restaurants: state.restaurData.restaurants,
loading: state.restaurData.loading
}
}
export default connect(mapStateToProps, {getAllRestaurants})(withLocalize(RestaurantCardColumns));
我的问题出在这一行:
this.props.getAllRestaurants(this.props.activeLanguage);
当我调试时,我可以看到activeLanguage 在render() 生命周期中可用。
在调用 getAllRestaurants
【问题讨论】:
-
在
mapStateToProps期间映射它(可以接受props参数),并且只有在连接器以正确的语言看到数据时才将loading设置为false。减速器中的 Thunk 可以处理该部分。
标签: javascript reactjs redux