console.log(action) 在 React Native 中不显示
console.log(action) does not display in React Native
我有多个减速器,这个减速器从未出现在任何 console.log()
中,但对于 default
情况,它总是转到另一个 xxx_fetch_success
有效负载。
这是我的 actions/index.js
export * from './ItemActions';
这是我的actions/types.js
export const ITEM_UPDATE = 'item_update';
export const ITEM_CREATE = 'item_create';
export const ITEMS_FETCH_SUCCESS = 'items_fetch_success';
这是我的actions/ItemActions.js
import firebase from 'firebase';
import { ITEM_UPDATE, ITEM_CREATE, ITEMS_FETCH_SUCCESS } from './types';
export const itemUpdate = ({ prop, value }) => {
return {
type: ITEM_UPDATE,
payload: { prop, value }
};
};
export const itemsFetch = () => {
return (dispatch) => {
firebase.database().ref(`items/`)
.on('value', snapshot => {
dispatch({ type: ITEMS_FETCH_SUCCESS, payload: snapshot.val() });
});
};
};
这是我的reducers/index.js
import { combineReducers } from 'redux';
import ItemFormReducer from './ItemFormReducer';
import ItemReducer from './ItemReducer';
export default combineReducers({
itemForm: ItemFormReducer,
items: ItemReducer,
});
这是我的reducers/ItemReducer.js
import { ITEMS_FETCH_SUCCESS } from '../actions/types';
const INITIAL_STATE = {}
export default (state = INITIAL_STATE, action) => {
switch (action.type){
case ITEMS_FETCH_SUCCESS:
console.log("ITEM_FETCH_SUCCESS: ",action);
return state;
default:
console.log("FROM DEFAULT: ",action);
return state;
}
};
这是我的 reducers/ItemFormReducer.js
import {
ITEM_UPDATE,
ITEM_CREATE,
} from '../actions/types';
const INITIAL_STATE = {
photo: '',
item: '',
category: '',
price: '',
description: '',
};
export default (state = INITIAL_STATE, action) => {
switch (action.type){
case ITEM_UPDATE:
return { ...state, [ action.payload.prop ]: action.payload.value };
case ITEM_CREATE:
return INITIAL_STATE;
default:
return state;
}
}
这是我的screens/Menu.js
import _ from 'lodash';
import React, { Component } from 'react';
import { Text, View, StyleSheet, TouchableOpacity } from 'react-native';
import { connect } from 'react-redux';
import { itemsFetch } from '../../actions';
class Menu extends Component {
componentDidMount() {
this.props.itemsFetch();
}
render() {
return (
<View>
<Text>Menu</Text>
<Text>Menu</Text>
<Text>Menu</Text>
<Text>Menu</Text>
<Text>Menu</Text>
<Text>Menu</Text>
</View>
);
}
}
export default connect(null, { itemsFetch })(Menu);
这是我的 Firebase 规则:
{
/* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
"rules": {
"items" : {
"$itemId":{
".read": true,
".write":"auth.uid === data.child('ownerId').val() || data.child('ownerId').val() === null"
}
},
"users":{
"$uid":{
".read": true,
".write":"$uid === auth.uid"
}
},
}
}
这是我的 Firebase 实时数据库结构:
ProjectName
- items
- users
问题是 reducers/ItemReducer.js 中的 console.log(action) 从未出现,但 default
中的 console.log(action)
与另一个对象数据负载一起出现
能帮帮我吗,先谢谢了。
在redux中使用React Native或Action没有问题。由于 firebase 安全规则,我阻止了所有写操作。
我有多个减速器,这个减速器从未出现在任何 console.log()
中,但对于 default
情况,它总是转到另一个 xxx_fetch_success
有效负载。
这是我的 actions/index.js
export * from './ItemActions';
这是我的actions/types.js
export const ITEM_UPDATE = 'item_update';
export const ITEM_CREATE = 'item_create';
export const ITEMS_FETCH_SUCCESS = 'items_fetch_success';
这是我的actions/ItemActions.js
import firebase from 'firebase';
import { ITEM_UPDATE, ITEM_CREATE, ITEMS_FETCH_SUCCESS } from './types';
export const itemUpdate = ({ prop, value }) => {
return {
type: ITEM_UPDATE,
payload: { prop, value }
};
};
export const itemsFetch = () => {
return (dispatch) => {
firebase.database().ref(`items/`)
.on('value', snapshot => {
dispatch({ type: ITEMS_FETCH_SUCCESS, payload: snapshot.val() });
});
};
};
这是我的reducers/index.js
import { combineReducers } from 'redux';
import ItemFormReducer from './ItemFormReducer';
import ItemReducer from './ItemReducer';
export default combineReducers({
itemForm: ItemFormReducer,
items: ItemReducer,
});
这是我的reducers/ItemReducer.js
import { ITEMS_FETCH_SUCCESS } from '../actions/types';
const INITIAL_STATE = {}
export default (state = INITIAL_STATE, action) => {
switch (action.type){
case ITEMS_FETCH_SUCCESS:
console.log("ITEM_FETCH_SUCCESS: ",action);
return state;
default:
console.log("FROM DEFAULT: ",action);
return state;
}
};
这是我的 reducers/ItemFormReducer.js
import {
ITEM_UPDATE,
ITEM_CREATE,
} from '../actions/types';
const INITIAL_STATE = {
photo: '',
item: '',
category: '',
price: '',
description: '',
};
export default (state = INITIAL_STATE, action) => {
switch (action.type){
case ITEM_UPDATE:
return { ...state, [ action.payload.prop ]: action.payload.value };
case ITEM_CREATE:
return INITIAL_STATE;
default:
return state;
}
}
这是我的screens/Menu.js
import _ from 'lodash';
import React, { Component } from 'react';
import { Text, View, StyleSheet, TouchableOpacity } from 'react-native';
import { connect } from 'react-redux';
import { itemsFetch } from '../../actions';
class Menu extends Component {
componentDidMount() {
this.props.itemsFetch();
}
render() {
return (
<View>
<Text>Menu</Text>
<Text>Menu</Text>
<Text>Menu</Text>
<Text>Menu</Text>
<Text>Menu</Text>
<Text>Menu</Text>
</View>
);
}
}
export default connect(null, { itemsFetch })(Menu);
这是我的 Firebase 规则:
{
/* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
"rules": {
"items" : {
"$itemId":{
".read": true,
".write":"auth.uid === data.child('ownerId').val() || data.child('ownerId').val() === null"
}
},
"users":{
"$uid":{
".read": true,
".write":"$uid === auth.uid"
}
},
}
}
这是我的 Firebase 实时数据库结构:
ProjectName
- items
- users
问题是 reducers/ItemReducer.js 中的 console.log(action) 从未出现,但 default
中的 console.log(action)
与另一个对象数据负载一起出现
能帮帮我吗,先谢谢了。
在redux中使用React Native或Action没有问题。由于 firebase 安全规则,我阻止了所有写操作。