React Redux,对象作为React子对象无效

时间:2019-12-02 08:17:48

标签: reactjs react-native react-redux

  

错误:对象作为React子对象无效(找到:对象与   键{badgesReducer,cityPickerReducer}),如果您打算呈现一个   子集,请改用数组。

试图修复此问题几天,但没有结果:“(。

注意:如果使用单个reducer(如果我未使用“ combineReducers({...})”),它们运行良好

我的动作:

1:

export const Picked = () =>{
    return{
       type: 'Picked',
       city: this.city
    }
}

2:

export const VIEW =()=>{
    return{
        type: 'flex' }
}

export const HIDE =()=>{
    return{
        type: 'none' }
}

我的减速器:

1:

export default (state= 'none', action) =>{
     switch(action.type){
        case 'VIEW':
          return 'flex';
        case 'HIDE':
          return 'none';
        default:
          return state; }
}

2:

export default (state="pick city", action) =>{
    switch(action.type){
        case 'Picked':
          return  action.city;
        default: {
          return state; }
     }
}

最后组合减速器,这是上面所述的错误发生:

import {combineReducers} from 'redux';
import badgesReducer from './badgesReducer';
import cityPickReducer from './cityPickReducer';

export default rootReducer = combineReducers({
   badgesReducer ,
   cityPickReducer
}); 

这是商店:

import {createStore} from 'redux';
import rootReducer from '../reducers/reducers'
export default store = createStore(rootReducer);

更新:

徽章组件:

1)主页组件:

imports ... 
import {connect} from 'react-redux';

class Home extends React.Component {
 render() {
     return (
      <View  style={styles.container} >
       <BloodList navigation={this.props.navigation }/>
      </View>
    ); }

  componentDidMount(){
   ...
    this._notificationSubscription = 
    Notifications.addListener(this._handleNotification);                
   }

   _handleNotification = (notification) => {
     ...
    if(notification.origin == 'received'){
            this.props.VIEW();
     ...}
  }

  const mapDispatchToProps=(dispatch)=>{
  return {
   VIEW:()=> dispatch({type: 'VIEW'})
  }
}

export default connect(null,mapDispatchToProps)(Home);

2)通知组件:

import ...
import {withNavigationFocus} from 'react-navigation';
import {connect} from 'react-redux';

class Notifications extends React.Component {

  componentDidUpdate(){
     if(this.props.isFocused){
          this.props.HIDE(); }
  }

  render() {   
    return (
      <View  style={styles.container} >
                  <NotificationsList navigation={this.props.navigation}/>  
      </View>
    ); }  
}
const mapDispatchToProps=(dispatch)=>{
  return {
   HIDE:()=> dispatch({type: 'HIDE'})
  }
}

export default connect(null,mapDispatchToProps)(withNavigationFocus(Notifications))

3)BarBadges组件

import ...
import {connect} from 'react-redux';

const NotificationsWithBadges =(props)=>{
    return <IconWithBadge {...props } {...props.badge.type}/>;
}

const mapStateToProps=(state) => {
    return {
        badge : state}
}
 export default connect(mapStateToProps)(NotificationsWithBadges);

4)徽章组件:

import...
export default...
render(){...
<Ionicons name={name} size={size} color={color} />
         {this.props.badge == 'flex' &&(
            <View style={{
      ....
}}

PickCity组件:

1)CreatPost组件:


import {connect} from 'react-redux';
var tempCity;
...

 class CreatePost...{
  ...
 render(){...}
}

const mapStateToProps=(state) => {
  return tempCity= { city : state }
}
export default connect(mapStateToProps)(CreatePost);

2)CitySearch组件:

import...
import {connect} from 'react-redux'

var pickedCity;
...
class CitySearch...{
 ...
 render(){...}
}

const mapDispatchToProps=(dispatch)=>{
    return {
     Picked:()=>dispatch({type:'Picked', city:pickedCity})
    } 
}
export default connect(null,mapDispatchToProps)(CitySearch);

2 个答案:

答案 0 :(得分:0)

没关系。但是我认为问题是由于没有声明rootReducer。 您可以尝试以下方法:

export const fetchProducts = () => async dispatch => {
  dispatch (fetchingProductsRequest ());
  try {
    let response = await api.get ('/ subcategory');
    console.log (response);
    let json = await response.json ();
    dispatch (fetchingProductsSucess (json));
  } catch (error) {
    dispatch (fetchingProductsFail (error));
  }
}

答案 1 :(得分:0)

使用默认导出时...这样做...

...
export default combineReducers({ ... });

或者...

const reducers = combineReducers({ ... });
export default reducers;