【问题标题】:ReactJS onClick in list item chage divReactJS onClick在列表项chage div中
【发布时间】:2021-05-02 04:47:17
【问题描述】:

我正在处理点击事件我有一个列表,我想在点击列表项时设置 onClick 事件。并显示特定列表的<div>

代码:

import React from "react";
    import { Container, Grid,Accordion } from "semantic-ui-react";
    
    
    class Powerups extends React.Component{
       constructor(props){
        super(props);
        this.state={
          activeIndex: 1
        }
        this.handleClick = this.handleClick.bind(this);
        this.handleChange = this.handleChange.bind(this);
       }
       componentWillMount() {}
       componentWillUnmount() {}
       componentDidMount() {}
       componentWillReceiveProps(nProps) {}
       
     
        handleClick(index) {
            const { activeIndex } = this.state
            const newIndex = activeIndex === index ? -1 : index
            this.setState({ activeIndex: newIndex })
        }
        handleChange(){
            
        }
    
    
        render() {
            const { activeIndex } = this.state
            return(
    
                <div className="powerups-root">
                   
    
                    <div style={{background: '#fff'}}>
                    <Container className="power-header-image">
                        <div style={{textAlign: 'center',marginTop: 32}}>
                        <h1><span style={{color: '#FF6701',fontWeight :'bold'}}>STACKBY POWERUPS</span></h1>
                        <h2><span style={{fontWeight :'bold'}}>Use powerups to make your lives easier.</span></h2>
                        </div>
                    </Container>
                    </div> 
    
            <Container style={{marginBottom: 75}}>
              <Grid>
                <Grid.Column mobile={16} tablet={4} computer={4}>
                  <div className="sidebar-bx">
                      <div className="sidebar-title">
                        <h2>categories</h2>
                      </div>
                      <ul className="sidebar-items" id="tags">
                        <li id="All"><span style={{ textDecoration: 'underline' }}>All</span></li> 
                        <li id="Import"><span>Import</span></li>
                        <li id="Sharing"><span>Sharing</span></li>
                        <li id="Data Recovery"><span>Data Recovery</span></li>
                        <li id="Data Transformation"><span>Data Transformation</span></li>
                        <li id="Automation"><span>Automation</span></li>
                        <li id="Apps"><span>Apps</span></li>
                      </ul>
                  </div>
                </Grid.Column>
                <Grid.Column container mobile={16} tablet={12} computer={12} container className="powerup" style={{marginLeft:'0px !important',height: 'fit-content', width: '100%'}}>
              
                  <Accordion className="stack-links">
                    <div className="card" id="Data Recovery">
                        <ul>
                            <li>
                            <Accordion.Title className="flex items-center" active={activeIndex === 1} index={1} onClick={() => this.handleClick(1)}>
                            Stack Export
                            </Accordion.Title>
                            </li>
                            <li>
                            <a className="live-lable" style={{ color: 'white', background: '#4FAD35', border: '2px solid #4FAD35',marginLeft:'10px', paddingBottom: '1px' }}>
                            Live
                            </a> 
                            </li>
                        </ul>
                        <Accordion.Content active={activeIndex === 1}>
                          <p>Export your complete stack back in an excel sheet .</p>
                        </Accordion.Content>
                    </div>
                    <div className="card" id="Data Transformation">
                      <ul>
                        <li>
                        <Accordion.Title className="flex items-center" active={activeIndex === 2} index={2} onClick={() => this.handleClick(2)}>
                        Append CSV file in an existing table
                        </Accordion.Title>
                        </li>
                        <li>
                        <a className="live-lable" style={{ color: 'white', background: '#4FAD35', border: '2px solid #4FAD35',marginLeft:'10px', paddingBottom: '1px' }}>
                          Live
                        </a> 
                        </li>
                        </ul>
                        <Accordion.Content active={activeIndex === 2}>
                          <p>Import a CSV file into an existing table and map the columns to take the form of your table.</p>
                        </Accordion.Content>
                    </div>
                    <div className="card" id="Import">
                      <ul>
                        <li>
                        <Accordion.Title className="flex items-center" active={activeIndex === 3} index={3} onClick={() => this.handleClick(3)}>
                        Google Sheet Importer
                        </Accordion.Title>
                        </li>
                        <li>
                        <a className="live-lable" style={{ color: 'white', background: '#FF6701', border: '2px solid #FF6701',marginLeft:'10px', paddingBottom: '1px' }}>
                          Coming soon     
                        </a> 
                        </li>
                        </ul>
                        <Accordion.Content active={activeIndex === 3}>
                          <p>Connect your Google Sheet Account and import your sheets data directly into your stacks.</p>
                        </Accordion.Content>
                    </div>         
                    <div className="card" id="Import">
                      <ul>
                        <li>
                        <Accordion.Title className="flex items-center" active={activeIndex === 4} index={4} onClick={() => this.handleClick(4)}>
                        Microsoft Excel Importer
                        </Accordion.Title>
                        </li>
                        <li>
                        <a className="live-lable" style={{ color: 'white', background: '#4FAD35', border: '2px solid #4FAD35',marginLeft:'10px', paddingBottom: '1px' }}>
                          Live
                        </a> 
                        </li>
                        </ul>
                        <Accordion.Content active={activeIndex === 4}>
                          <p>Import an excel file to create a new stack or import it to a table.</p>
                        </Accordion.Content>
                    </div>          
                    <div className="card" id="Sharing">
                      <ul>
                        <li>
                        <Accordion.Title className="flex items-center" active={activeIndex === 5} index={5} onClick={() => this.handleClick(5)}>
                        View level sharing
                        </Accordion.Title>
                        </li>
                        <li>
                        <a className="live-lable" style={{ color: 'white', background: '#4FAD35', border: '2px solid #4FAD35',marginLeft:'10px', paddingBottom: '1px' }}>
                          Live
                        </a> 
                        </li>
                        </ul>
                        <Accordion.Content active={activeIndex === 5}>
                          <p>Share individual views with anyone in your team - only share filtered rows, or even hide columns.</p>
                        </Accordion.Content>
                    </div>  
                    <div className="card" id="Data Recovery">
                      <ul>
                        <li>
                        <Accordion.Title className="flex items-center" active={activeIndex === 6} index={6} onClick={() => this.handleClick(6)}>
                        Recycle Bin
                        </Accordion.Title>
                        </li>
                        <li>
                        <a className="live-lable" style={{ color: 'white', background: '#4FAD35', border: '2px solid #4FAD35',marginLeft:'10px', paddingBottom: '1px' }}>
                          Live
                        </a> 
                        </li>
                        </ul>
                        <Accordion.Content active={activeIndex === 6}>
                          <p>Recover deleted items (Tables, Views, Rows and Columns) from last 7 days.</p>
                        </Accordion.Content>
                    </div>   
                    <div className="card" id="Data Transformation">
                      <ul>
                        <li>
                        <Accordion.Title className="flex items-center" active={activeIndex === 7} index={7} onClick={() => this.handleClick(7)}>
                        Deduplication
                        </Accordion.Title>
                        </li>
                        <li>
                        <a className="live-lable" style={{ color: 'white', background: '#FF6701', border: '2px solid #FF6701',marginLeft:'10px', paddingBottom: '1px' }}>
                          Coming soon
                        </a> 
                        </li>
                        </ul>
                        <Accordion.Content active={activeIndex === 7}>
                          <p>Import data and manage duplicate rows in the same table.</p>
                        </Accordion.Content>
                    </div>     
                    <div className="card" id="Automation">
                      <ul>
                        <li>
                        <Accordion.Title className="flex items-center" active={activeIndex === 8} index={8} onClick={() => this.handleClick(8)}>
                        Automate your API column type
                        </Accordion.Title>
                        </li>
                        <li>
                        <a className="live-lable" style={{ color: 'white', background: '#4FAD35', border: '2px solid #4FAD35',marginLeft:'10px', paddingBottom: '1px' }}>
                          Live
                        </a> 
                        </li>
                        </ul>
                        <Accordion.Content active={activeIndex === 8}>
                          <p>Set automated schedules (daily, 3 days, weekly, biweekly, monthly) on your API column type and keep yourself up to date at all times. Note: Please check total limits based on your plan.</p>
                        </Accordion.Content>
                    </div>    
                    <div className="card" id="Apps">
                      <ul>
                        <li>
                        <Accordion.Title className="flex items-center" active={activeIndex === 9} index={9} onClick={() => this.handleClick(9)}>
                        Google Drive Sync
                        </Accordion.Title>
                        </li>
                        <li>
                        <a className="live-lable" style={{ color: 'white', background: '#4FAD35', border: '2px solid #4FAD35',marginLeft:'10px', paddingBottom: '1px' }}>
                          Live
                        </a> 
                        </li>
                        </ul>
                        <Accordion.Content active={activeIndex === 9}>
                          <p>Sync your stacks directly with your Google Drive Account and open/create stacks from Google Drive.</p>
                        </Accordion.Content>
                    </div>
                    <div className="card" id="Apps">
                      <ul>
                        <li>
                        <Accordion.Title className="flex items-center" active={activeIndex === 10} index={10} onClick={() => this.handleClick(10)}>
                        Google Chrome Extension
                        </Accordion.Title>
                        </li>
                        <li>
                        <a className="live-lable" style={{ color: 'white', background: '#4FAD35', border: '2px solid #4FAD35',marginLeft:'10px', paddingBottom: '1px' }}>
                          Live
                        </a> 
                        </li>
                        </ul>
                        <Accordion.Content active={activeIndex === 10}>
                          <p>Clip information from any webpage, through a custom google chrome extension <a href="#">Download it from here</a>.</p>
                        </Accordion.Content>
                    </div>     
                </Accordion>
    
                
                </Grid.Column>
            </Grid>
    
              
                  </Container>
    
           
    
           </div>
            )
        }
    }
    
    const mapStateToProps = ({ auth }) => {
        return { auth }
    }
    
    export default connect(mapStateToProps, {})(Powerups);

我希望当我点击像 import 这样的特定类别时,它会显示 import div

所以,我不明白这个,我会新尝试这个 请任何人帮忙

谢谢!

【问题讨论】:

  • 你能分享你的 React 组件吗?您共享的代码基本上是包含在反引号中的静态 HTML,因此它不提供有关您如何尝试处理您描述的功能的任何信息。
  • 顺便说一句,我建议不要使用包含空格的id 属性
  • 对不起;我不明白你的最后两个cmets;你能不能再详细一点,好吗?
  • @secan 请检查我的代码我也将在手风琴上工作
  • React 不能通过 id 查询工作(不像 jQuery)。如果你是 React 新手,我建议你写最新的function components

标签: javascript reactjs


【解决方案1】:

您可以为列表项创建一个对象数组,然后将列表信息传递给子组件并显示内容。

这是一个关于如何实现目标的工作示例,

代码沙盒 => https://codesandbox.io/s/frosty-bird-vqzzp?file=/src/App.js:0-892

import React, { useState } from "react";

import "./styles.css";

const listItems = [
  { id: "0", title: "One", content: " I AM ONE" },
  { id: "1", title: "Two", content: " I AM TWO" },
  { id: "2", title: "Three", content: " I AM THREE" }
];

const ListView = ({listId}) => {
  const listView = listItems.map((item) => {
    if (item.id === listId) {
      return <span>{item.content}</span>;
    }
  });

  return <div>{listView}</div>;
};

const App = () => {
  const [showListView, setShowListView] = useState(false);
  const [listId, setListId] = useState(null);

  const listClickHandler = (id) => {
    setListId(id);
  };

  const list = listItems.map((item) => {
    return <li onClick={() => listClickHandler(item.id)}>{item.title}</li>;
  });

  return (
    <div className="App">
      <ul>{list}</ul>

      <ListView listId={listId} />
    </div>
  );
};

export default App;

【讨论】:

  • 尝试使用上面建议的代码并根据您的需要进行修改。
猜你喜欢
  • 2017-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多