Okey, after several hours of searching for the solution, i need to post a question about this.
Ive read x amounts of threads and tried "connecting" the component with state, different promise callbacks, ive tried setTimeout (big nono, i know), ive tried updating on componentDidMount, componentWillMount, componentDidUpdate etc. Ive tried forceUpdate(). In fact, state is uppdated and contains the "productlist" with updated data from Firebase. It seems as though the component renders without this data.
Function fetching data from firebase:
function updateProducts(endpointstring) {
let initialList = [];
let productsList = [];
function getData(endpoint) {
let productsRef = database.ref('products/' + endpoint);
return productsRef.once('value').then(function (snapshot) {
return snapshot.val()
})
}
Promise.all([getData(endpointstring)]).then( (response) => {
for(var product in response){
productsList.push(response[product])
console.log(productsList)
}
})
return productsList;
}
Reducer:
import * as firebase from 'firebase';
const database = firebase.database();
function updateProducts(endpointstring) {
let initialList = [];
let productsList = [];
function getData(endpoint) {
let productsRef = database.ref('products/' + endpoint);
return productsRef.once('value').then(function (snapshot) {
let list = [];
for(var prod in snapshot.val()){
list.push(snapshot.val()[prod])
}
return list
})
}
var newList = getData('mobiles');
for(var product in newList){
productsList.push(newList[product])
console.log(productsList)
}
return getData('mobiles');;
}
const mobilesReducer = (state = {
view: "everything",
filteredValue: "",
productInFocus: 0,
products: updateProducts('mobiles'),
imgLoads: [],
loadedOnce: false,
priceRange: {min: 0, max: 3500},
minPrice: 0,
maxPrice: 3500,
sort: "popular"
}, action) => {
let newState = {...state};
switch(action.type){
case 'COMPONENT_DID_MOUNT':
console.log(action.payload)
action.payload.componentName === 'mobiles' ? newState = {...newState, products: action.payload.productList} : null;
return newState;
Anybody has a different take in this?
Thanks.