亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

單擊時未觸發 onClick 事件 - 反應

單擊時未觸發 onClick 事件 - 反應

慕蓋茨4494581 2021-12-02 19:16:13
我是超級新手,我正在努力讓我的 onClick 工作。當頁面呈現時,我看到它被調用(多次,取決于數組中的項目數)但不是點擊。const BlogPage = props => {          const posts = props.data.allContentfulPost.edges;          const postsStable = props.data.allContentfulPost.edges;          const categoriesStyle = {            marginBottom: '8px',          };          const filterCategories = category => {            console.log('here', category);            // this.posts = this.posts.filter(p => p.catergory === category);          };.        return (        <div style={categoriesStyle}>            {posts.map(({ node: c }) => (              <Badge onClick={filterCategories(c.category)}                  key={c.id}                  category={c.category}                  color="#fff">                  {c.category}               </Badge>             ))}         </div>        );    };我試過了:onClick={() => filterCategories(c.category)} 和onClick={() => {                  filterCategories(c.category);                }}const Badge = ({ children, category = 'Other', color = '#4a4a4a' }) => {  return (    <Container background={pillColor(category)} color={color}>      {children}    </Container>  );};Badge.propTypes = {  children: PropTypes.node.isRequired,  category: PropTypes.string,  color: PropTypes.string,};根據答案,問題似乎是 Badge 不是 HTML 元素。
查看完整描述

2 回答

?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

傳遞給的值onClick需要是一個函數。

filterCategories(c.category)filterCategories 立即調用并將其返回值(undefined因為沒有return語句)傳遞給onClick.

undefined 不是函數。

創建一個filterCategories使用您想要的參數調用的函數(例如 with bind,或者只使用函數表達式或箭頭函數)并傳遞它。


此外,Badge不是 HTML 元素。如果您的組件沒有將onClickprop傳遞給 HTML 元素,則它不會在 DOM 中執行任何操作。


查看完整回答
反對 回復 2021-12-02
?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

<div style={categoriesStyle}>

                {posts.map(({ node: c }) => (

                  <span

                    tabIndex="0"

                    key={c.id}

                    onClick={() => filterCategories(c.category)}

                    role="button"

                    onKeyDown={filterCategories}

                  >

                    <Badge value={c.category} category={c.category} color="#fff">

                      {c.category}

                    </Badge>

                  </span>

                ))}


查看完整回答
反對 回復 2021-12-02
  • 2 回答
  • 0 關注
  • 215 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號