0
我想在某個階段派遣一個行動到減速機,但它似乎並沒有工作。 showGeo()函數在組件掛載時啓動。我在控制檯中看到的是一個空數組,這是默認狀態。我試圖用dispatch包裝返回塊中的代碼,但這也不起作用。我是初學者,所以任何幫助將不勝感激。我試圖派遣一個行動,但它似乎並沒有註冊
export const sendAction = (user) => {
return (dispatch) => {
dispatch({ type: GEO_SUCCESS, payload: user });
};
};
export const showGeo =() => {
const firebaseRef = firebase.database().ref('/geofire');
const geoFire = new geofire(firebaseRef);
let user;
const geoQuery = geoFire.query({
center: [37.78, -122.4],
radius: 10 //kilometers
});
geoQuery.on("key_entered", function(key, location, distance) {
console.log("Bicycle shop " + key + " found at " + location + " (" + distance + " km away)");
firebase.database().ref('/users/' + key).once('value').then(function(snapshot) {
console.log(snapshot.val());
user = snapshot.val();
sendAction(user);
});
});
};
這裏是減速文件:
import {
GEO_SUCCESS
} from '../actions/types';
export default (state = [], action) => {
switch (action.type) {
case GEO_SUCCESS:
return action.payload;
default:
return state;
}
};
嗨,我忘了提及該用戶是一個全局變量可由sendAction訪問。無論如何,我會嘗試你的建議。謝謝 – moe
好奇....是否有一個原因,它是一個全局變量?您正在使用redux並分派一個操作以將用戶包含在其中。 –
沒有真正的理由,我改變了代碼,按照你的建議提供它作爲參數,並且動作仍然沒有更新用戶的狀態。 – moe