【问题标题】:React: onClick not rendering on div反应:onClick 不在 div 上呈现
【发布时间】:2018-10-31 03:07:38
【问题描述】:

更新:结果一切正常。我的 className 三元组中出现了一个错误,导致该类不适用。但是,有人可以解释为什么 onClick 没有出现在检查器中的<div> 上吗?

我有一个反应组件,我想在其中渲染一个带有onClickevent 的<div>。但是,由于某种原因,onClick 没有呈现。除了警告之外,我在控制台中没有任何错误:Each child in an array or iterator should have a unique "key" prop.

实际渲染的<div> 如下所示:<div id="1" class="unselected">Peppered Chicken</div>

我的代码如下所示。想法?

import React from 'react'
import ReactDOM from 'react-dom'
import PropTypes from 'prop-types'

class App extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (<div><RecipesList/></div>)
    }
}

class RecipesList extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            recipes: [{recipes:[]}],
            selectedRecipe: null
        };

        this.setRecipeAsSelected = this.setRecipeAsSelected.bind(this)
    }

    componentDidMount() {
        fetch('/recipes')
            .then(response => response.json())
            .then(recipes => this.setState({recipes}));
    }

    setRecipeAsSelected(recipeID) {
        this.setState({selectedRecipe: recipeID})
    }

    render() {
        return (
            <div>
                {this.state.recipes[0]["recipes"].map((recipe, index) => {
                    return <Recipe setRecipeAsSelected={this.setRecipeAsSelected} selectedRecipe={this.state.selectedRecipe} recipe={recipe} id={index}/>
                })}
                <Generate/>
            </div>
        )
    }
}

class Recipe extends React.Component {
    constructor(props){
        super(props);

        this.setThisAsSelected = this.setThisAsSelected.bind(this)
    }

    setThisAsSelected() {
        this.props.setRecipeAsSelected(this.props.id)
    }

    render() {
        return (
            <div onClick={this.setThisAsSelected} key={this.props.id} id={this.props.id} className={this.props.selectedRecipe === this.props.key ? "bg-primary" : "test"}> {this.props.recipe} </div>
        )
    }
}

class Generate extends React.Component {
    render() {
        return (
            <div>
                <button>GENERATE</button>
            </div>
        )
    }
}

document.addEventListener('DOMContentLoaded', () => {
    ReactDOM.render(
        <App/>,
        document.body.appendChild(document.createElement('div')),
    )
});

【问题讨论】:

  • Each child in an array or iterator should have a unique "key" prop 要解决此问题,请在映射到 &lt;Recipe /&gt; 组件时,在 RecipleList 的渲染方法中添加键。即&lt;Recipe ... key={index} /&gt;
  • class="unselected" 来自哪里?它不在代码中。
  • 我猜应该是“测试”。为了清楚起见,我更改了它,但忘记在元素上更新它
  • 上述更新中的解决方案
  • @JonnyB 刚刚更新了答案,以包括按要求添加的详细信息 :-)

标签: javascript reactjs onclick


【解决方案1】:

这种问题可能是由于key 属性未在列表项上呈现(即,如警告所示)。 key 在这里很重要的原因是 React 使用唯一的 per-item key prop 来正确解析/确定如何在渲染周期中更新列表中渲染的项目(即通过map())。

合并key 属性的一种简单方法是使用从地图回调传入的唯一“索引”,如下所示:

render() {
    return (
        <div>
            {this.state.recipes[0]["recipes"].map((recipe, index) => {
                return <Recipe 
                        setRecipeAsSelected={this.setRecipeAsSelected}
                        selectedRecipe={this.state.selectedRecipe} 
                        recipe={recipe} id={index} key={index }/> 
            })}
            <Generate/>
        </div>
    )
}

更新

此外,您在呈现的 HTML 中看不到 onClick 属性(即可以在 dev-tools 中看到)的原因是因为 dev-tools 显示的呈现 HTML 不是您实际显示的 JSX在组件的 render() 方法中定义。

您定义的 JSX 实际上在幕后转换为 javascript,因此,onClick 处理程序会附加到该 &lt;div /&gt; 的相应 javascript Node 对象(这是在内部通过 addEventListener() 完成的) .因此,这使得将 onClick 处理程序渲染到 HTML 标记中变得多余。

希望有帮助!

【讨论】:

  • 谢谢。在解决这个问题时,我意识到我在设置我的类名时搞砸了三元组。我最初试图将此 key={index} 作为道具传递给每个
    一个唯一的键并在 className 三元组中使用。
  • 但是,我收到一个错误,因为您不能将 key= 作为道具传递,所以我只是删除了该道具并使用了 id。直到您的建议,我才意识到为什么会出现该错误。创建道具和设置键的语法是相同的。尝试使用 key= 作为道具只需在 上设置键值。从 中删除 key= 后,我没有将 className 三元组从 this.props.key 更改为 this.props.id。感谢您的帮助。
  • @JonnyB 谢谢你 - 很高兴听到它正在工作!
  • 你知道为什么 onClick 没有出现在检查器中吗?
  • 您使用元素选项卡对吗?它根本不在其中的 div 上。
猜你喜欢
相关资源
最近更新 更多
热门标签