Как заставить onClick в React работать с одним элементом с несколькими братьями и сестрами? - PullRequest
1 голос
/ 02 августа 2020

Я новичок в React, и у меня возникла проблема с попыткой запустить событие onClick. У меня работает событие, когда по нему щелкают, появляется и снова появляется div. Проблема в том, что если я нажимаю кнопку для определенного элемента, все div появляются вместо того div, на котором я только что нажал кнопку. Как сделать так, чтобы кнопка, на которую я нажал, срабатывала только для этого конкретного элемента.

Вот мой код:

class App extends React.Component {

  constructor(props) {
    super(props)
    this.state = {
      userInput: '',
      getRecipe: [],
      ingredients: "none"
    }
  }

  handleChange = (e) => {
    this.setState({
      userInput: e.target.value
    })
  }
  

  handleSubmit = (e) => {
    e.preventDefault()

    const getData = () => {
      fetch(`https://api.edamam.com/search?q=${this.state.userInput}&app_id=${APP_ID}&app_key=${APP_KEY}&from=0&to=18`)
        .then(res => {
          return res.json()
        }).then(data => {
          this.setState({
            getRecipe: data.hits
          })
        })
    }
    getData()
  }
// this is where the button logic comes in
  getIngredients = (e) => {
    e.preventDefault()
    if (this.state.ingredients === 'none') {
      this.setState({
        ingredients: "block"
      })
    } else {
      this.setState({
        ingredients: "none"
      })
    }
  }


  render() {

    return (
      <div className="recipes">
        <Nav changed={this.handleChange} submit={this.handleSubmit} />
        <Content
          userInput={this.state.userInput}
          recipe={this.state.getRecipe}
          getIngredients={this.getIngredients}
          ingredients={this.state.ingredients} />
      </div>
    )
  }
}

const Content = ({ userInput, recipe, getIngredients, ingredients }) => {

    return (
        <div>
            <h2 className="userinputtitle"> {userInput} </h2>
            <div className="containrecipes">
                {recipe.map(rec => {
                    return (
                        <div key={rec.recipe.label} className="getrecipes">
                            <h1 className="recipetitle" key={rec.recipe.label}>{rec.recipe.label.toUpperCase()}</h1>
                            <img src={rec.recipe.image}></img>
                            <h4 className="health"> Health Labels: {rec.recipe.healthLabels.join(', ')}</h4>
                            <h4 > Diet Label: {rec.recipe.dietLabels}</h4>
                            <h4 > Calories: {Math.floor(rec.recipe.calories)}</h4>
                            <h4 className="cautions"> Cautions: {rec.recipe.cautions}</h4>
                            <div>
                                <h4>{rec.recipe.digest[0].label + ":" + " " + Math.floor(rec.recipe.digest[0].total) + "g"}</h4>
                                <h4>{rec.recipe.digest[1].label + ":" + " " + Math.floor(rec.recipe.digest[1].total) + "g"}</h4>
                                <h4>{rec.recipe.digest[2].label + ":" + " " + Math.floor(rec.recipe.digest[2].total) + "g"}</h4>
                            </div>
// the button is clicked here, yet all div fire at the same time
                            <button onClick={getIngredients} className="getingredients">Ingredients</button>
                            {rec.recipe.ingredients.map(i => {
                                return (
                                    <div style={{ display: ingredients }} className="containingredients">
                                        < ul className="ingredients">
                                            <li className="ingredient">{i.text}</li>
                                        </ul>
                                    </div>
                                )

                            })}
                        </div>

                    )
                })}
            </div>
        </div>

    )
}

1 Ответ

1 голос
/ 02 августа 2020

Обновите getIngredients, чтобы использовать также идентификатор рецепта и вместо этого сохранить его в состоянии.

Переключить ингредиент одного рецепта

this.state = {
  userInput: '',
  getRecipe: [],
  ingredientsId: null
}

...

getIngredients = recipeId => e => {
  e.preventDefault();
  this.setState(prevState => ({
    ingredientsId: prevState.ingredientsId ? null : recipeId,
  }));
}

...

<Content
  userInput={this.state.userInput}
  recipe={this.state.getRecipe}
  getIngredients={this.getIngredients}
  ingredientsId={this.state.ingredientsId} // <-- pass id
/>

Условно установить стиль отображения в Content.

const Content = ({ userInput, recipe, getIngredients, ingredientsId }) => {

  ...

  <button
   onClick={getIngredients(recipe.id)} // <-- pass id
   className="getingredients"
  >
    Ingredients
  </button>
  <div
   style={{
     // set display style
     display: ingredientsId === recipe.id ? "block" : "none"
   }}
   className="containingredients"
  >
    <ul className="ingredients">
      <li className="ingredient">{i.text}</li>
    </ul>
  </div>

  ...

Переключить несколько ингредиентов рецепта

То же, что и выше, с небольшими изменениями

Состояние - карта

this.state = {
  userInput: '',
  getRecipe: [],
  ingredientsId: {},
}

Переключить идентификатор в обработчике

getIngredients = recipeId => e => {
  e.preventDefault();
  this.setState(prevState => ({
    ingredientsId: {
      ...prevState.ingredientsId,
      [recipeId]: !prevState.ingredientsId[recipeId]
    },
  }));
}

Найти recipeId на пройденной карте

style={{
  // set display style
  display: ingredientsId[recipe.id] ? "block" : "none"
}}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...