I've the following component structure.
<Dashboard>
<UserData />
<MealList />
<Search />
</Dashboard>
I'm fetching the contents for UserData and MealList using useEffects inside dashboard and passing them down as props.
const [username, setUsername] = useState("");
const [email, setEmail] = useState("");
const [calories, setCalories] = useState("");
const [meals, setMeals] = useState([]);
useEffect(async () =>{
const data = await fetchUserData(localStorage.getItem('email'));
localStorage.setItem('user_id', data['id']);
setUsername(data['username']);
setEmail(data['email']);
setCalories(data['calories']);
const mealsData = await fetchMeals(localStorage.getItem('user_id'));
setMeals([...meals, ...mealsData]);
}, []);
Now what I want to implement is based on search parameters the meal list refreshes.
I'm grabbing the search parameters from the Search component and passing it back to the MealList.
Then I make a REST call and fetch the updated data.
const updateList = async ( ft, tt, fd, td) =>{
const userId = localStorage.getItem('user_id');
const updatedMealsList = await searchMeals(userId, ft, tt, fd, td);
console.log('Updated Meals', updatedMealsList);
setMeals([]);//Tried this but doesn't work
setMeals([...meals, ...updatedMealsList]);//
};
My question is how do I update the Meallist array and re-render the component?
I've already tried this, and it doesn't work. What I mean by doesn't work is neither is data in the meals array updated nor is it being re-rendered.
Here are the components.
const Dashboard = () => {
const [username, setUsername] = useState("");
const [email, setEmail] = useState("");
const [calories, setCalories] = useState("");
const [meals, setMeals] = useState([]);
useEffect(async () =>{
const data = await fetchUserData(localStorage.getItem('email'));
localStorage.setItem('user_id', data['id']);
setUsername(data['username']);
setEmail(data['email']);
setCalories(data['calories']);
const mealsData = await fetchMeals(localStorage.getItem('user_id'));
setMeals([...meals, ...mealsData]);
}, []);
const updateList = async ( ft, tt, fd, td) =>{
const userId = localStorage.getItem('user_id');
const updatedMealsList = await searchMeals(userId, ft, tt, fd, td);
setMeals([]);
console.log('Updated Meals', updatedMealsList);
setMeals([...meals, ...updatedMealsList]);
};
console.log('Updated Meals', meals);
return (
<div>
<Navigation/>
<div className="wrapper">
<div style={{display: 'flex', flexFlow: 'row wrap'}}>
<UserData username={username} email={email} calories={calories}/>
<Search onSubmit={updateList}/>
</div>
<MealList meals={meals}/>
</div>
</div>
)
};
export default Dashboard;
Meallist
const MealList = (props) => {
return (
<div style={{width: '70%'}}>
<h3 style={{color: 'grey', fontSize: '1.4em', fontWeight: '700', marginBottom: '1em'}}><FontAwesomeIcon
icon={faPlusCircle} style={{color: '#007bff', marginRight: '0.5em'}}/> ADD MEAL</h3>
<Table striped bordered hover>
<thead>
<tr>
<th>#</th>
<th>Meal</th>
<th>Calories</th>
<th>Time</th>
<th>Date</th>
<th>Update</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{props.meals.map((meal, index) => (<Meal count={index +1} meal={meal}/>))}
</tbody>
</Table>
</div>
)
};
export default MealList;
mealsarray because I have seen onlymealsData? Also try to use React dev tools to inspect the aforementioned arrays/data.setMealsfunction down as a prop fromDashboardto theMeallistcomponent? From reading the question I'm assuming theupdateListcall is coming from there... once you pass the function down to the correct component it should work just fine.useEffectan async function should be giving a warning.