【发布时间】:2021-01-12 07:41:50
【问题描述】:
我正在尝试通过 id 从对象内的数组中删除一个对象。我不知道该怎么做……我看过其他几篇帖子,但我觉得设置有点不同,所以我还是不明白。
deleteMeals 不会删除正确的对象,即使我获取了正确的餐点 ID。 “星期一”也作为占位符/试图解决问题。
我之前的 store 设置不同,每个餐点对象都有自己的 day 键,我可以删除 - 除了第一个对象 (id: 1) 直到最后一个对象才会删除一。我最终重构了商店,以便每个用餐对象都在一个由实际 Day 分隔的数组中。
所以现在我需要帮助解决两个问题!
- 如何根据我的商店对象设置正确删除
- 为什么正确的饭菜不会删除
const store = {
meals: {
"Monday": [
{
"id": 1,
"recipe_id": 2,
"user_id": 1
},
{
"id": 2,
"recipe_id": 1,
"user_id": 1
},
{
"id": 3,
"recipe_id": 3,
"user_id": 1
}
],
"Tuesday": [
{
"id": 4,
"recipe_id": 4,
"user_id": 1
}
],
"Wednesday": [],
}
}
在 App.js 中删除餐食
deleteMeal = (day, mealId) => {
this.setState(prevState => ({
// copy existing state
...prevState,
// update meals key
meals: {
// copy existing meals state
...prevState.meals,
// update day key & filter meals array by id
[day]: prevState.meals[day].filter( ({ id }) => id !== mealId),
}
}));
}
const contextValue = {
recipes: this.state.recipes,
meals: this.state.meals,
addRecipe: this.addRecipe,
deleteRecipe: this.deleteRecipe,
updateRecipe: this.updateRecipe,
addMeal: this.addMeal,
deleteMeal: this.deleteMeal
}
天数组件
class Days extends React.Component{
render() {
const { day, meals } = this.props
let mealsList;
if (meals[day]) {
mealsList = meals[day]
.map((meal, key) =>
<Meals
key={key}
day={day}
meal={meal}
/>
)
}
return(
<div className="Days">
<h3>{day}</h3>
{meals && meals[day] && mealsList}
</div>
)
}
}
膳食组件
class Meals extends React.Component {
static contextType = StashContext;
state = {
recipeTitle: 'No Title',
recipeImageUrl: '',
showModal: false
}
findRecipe = (recipeId, recipes) => {
const recipe = recipes.find( recipe => recipe.id == recipeId)
if (recipe) {
this.setState({
recipeTitle: recipe.title,
recipeImageUrl: recipe.image_url
})
}
}
componentDidMount() {
const { recipes } = this.context
const { meal } = this.props
this.findRecipe(meal.recipe_id, recipes)
}
handleClickDelete = (event) => {
const { day, meal } = this.props
const { id } = meal
event.preventDefault()
this.context.deleteMeal(day, id)
}
toggleModal = () => {
this.setState({
showModal: !this.state.showModal
})
}
render(){
const { recipeTitle, recipeImageUrl, showModal } = this.state;
const { meal } = this.props;
const customStyles = {
overlay: {
background: '#4c645682'
},
content: {
background: 'rgb(240 240 240)'
}
}
const permissions = {
edit: false,
add: false,
delete: false
}
return (
<div className="Meals">
{showModal &&
<DisplayModal
meal={meal}
customStyles={customStyles}
showModal={showModal}
closeModal={this.toggleModal}
label="Meal Modal"
permissions={permissions}
/>
}
<div className="Meals__info">
<div className="info_box left">
<div className="Meals__img">
<img src={recipeImageUrl} alt="Placeholder" />
</div>
</div>
<div className="info_box middle">
<div className="Meals__recipe-title">
{recipeTitle}
</div>
</div>
<div className="info_box right">
<div className="Meals__options">
<FontAwesomeIcon
icon={faEye}
onClick={e => {this.setState({showModal: true})}} />
<FontAwesomeIcon
icon={faBackspace}
onClick={this.handleClickDelete} />
</div>
</div>
</div>
</div>
)
}
}
export default Meals
【问题讨论】:
-
为什么需要餐食和食谱 ID?一天中的两个元素可以有相同的用餐ID吗?
-
我想我已经回答了 (2) 但对于 (1) 我认为我们需要更多关于您的商店如何与您的 UI(即组件)相关的上下文,大概与删除功能相关,虽然目前尚不清楚为什么您可能有两个事实来源。
标签: javascript arrays reactjs nested javascript-objects