最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make List Items clickable individually from one another in react.js - Stack Overflow

programmeradmin5浏览0评论

I am trying to make each individual list item clickable. Here I have a color change state so that the color changes to red. But everytime I click one list item, it makes all of the boxes red. Again I want to select which ones to turn red, not turn all of them red. a hint in the right direction or a link would do fine.

 import React, { Component } from 'react';
    import './App.css';
    import MainTool from './ponents/Layout/MainTool/MainTool.js';
    import Aux from './hoc/Aux.js';

    class App extends Component {

      state = {
        MainList: [
          "GamePlay",
          "Visuals",
          "Audio",
          "Story"
        ],
        color: "white"
      }

      changeEle = () =>{
        this.setState({color : "red"});
      }

      render() {
        return (
          <Aux>
            <MainTool MainList = {this.state.MainList}
            change = {this.changeEle}
            color = {this.state.color}/>
          </Aux>
        );
      }
    }

    export default App;

This MainTool just shoots my state arguments to Checkblock. Just for reference. Its unlikely the error is here although I have been wrong plenty times before.

import React from 'react';
import './MainTool.css';
import CheckBlock from '../../CheckBlock/CheckBlock';

const MainTool = props => {
    return (
        <div className = "mtborder">
            <CheckBlock MainList = {props.MainList}
            change = {props.change}
            color = {props.color}/>
        </div>
    );
};

export default MainTool;

And here is my best guess at where the problem is. I used a loop to iterate through my state object array and print out the list and divs next to each list item. the divs being the elements I want to make clickable individually.

import React from 'react';
import './CheckBlock.css';
import Aux from '../../hoc/Aux';

const CheckBlock = props => {

console.log(props.change);
console.log(props.color);

    let mainList = [];

    for(let i = 0; i <= 3; i++)
    {
        mainList[i] = <li key = {i} 
        className = "nameBox">{props.MainList[i]}
        <div onClick = {props.change} 
        style = {{backgroundColor: props.color}} 
        className = "clickBox"></div></li>
    }

    //console.log(dubi());

    return (
        <Aux>
            <ul className = "mainList">{mainList}</ul>
            <button>Enter</button>
        </Aux>
    );
};

export default CheckBlock;

I am trying to make each individual list item clickable. Here I have a color change state so that the color changes to red. But everytime I click one list item, it makes all of the boxes red. Again I want to select which ones to turn red, not turn all of them red. a hint in the right direction or a link would do fine.

 import React, { Component } from 'react';
    import './App.css';
    import MainTool from './ponents/Layout/MainTool/MainTool.js';
    import Aux from './hoc/Aux.js';

    class App extends Component {

      state = {
        MainList: [
          "GamePlay",
          "Visuals",
          "Audio",
          "Story"
        ],
        color: "white"
      }

      changeEle = () =>{
        this.setState({color : "red"});
      }

      render() {
        return (
          <Aux>
            <MainTool MainList = {this.state.MainList}
            change = {this.changeEle}
            color = {this.state.color}/>
          </Aux>
        );
      }
    }

    export default App;

This MainTool just shoots my state arguments to Checkblock. Just for reference. Its unlikely the error is here although I have been wrong plenty times before.

import React from 'react';
import './MainTool.css';
import CheckBlock from '../../CheckBlock/CheckBlock';

const MainTool = props => {
    return (
        <div className = "mtborder">
            <CheckBlock MainList = {props.MainList}
            change = {props.change}
            color = {props.color}/>
        </div>
    );
};

export default MainTool;

And here is my best guess at where the problem is. I used a loop to iterate through my state object array and print out the list and divs next to each list item. the divs being the elements I want to make clickable individually.

import React from 'react';
import './CheckBlock.css';
import Aux from '../../hoc/Aux';

const CheckBlock = props => {

console.log(props.change);
console.log(props.color);

    let mainList = [];

    for(let i = 0; i <= 3; i++)
    {
        mainList[i] = <li key = {i} 
        className = "nameBox">{props.MainList[i]}
        <div onClick = {props.change} 
        style = {{backgroundColor: props.color}} 
        className = "clickBox"></div></li>
    }

    //console.log(dubi());

    return (
        <Aux>
            <ul className = "mainList">{mainList}</ul>
            <button>Enter</button>
        </Aux>
    );
};

export default CheckBlock;
Share Improve this question asked May 26, 2018 at 0:19 chreechree 2581 gold badge3 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You need a state based ListItem ponent with internal color state. No need to pass function as prop to change color. Use internal method

class ListItem extends Component {
  state = { color : 'white' };
  onClick = () => {
     this.setState({ color: 'red' });
  }
  render () {
       return (
        <li key={i} className="nameBox">
            {this.props.value}
         <div onClick={this.onClick} style={{backgroundColor: props.color}} 
    className="clickBox">
        </div>
       </li>
   );
  }
}


const CheckBlock = props => {

   console.log(props.change);
   console.log(props.color);

    let mainList = [];

    for(let i = 0; i <= 3; i++)
    {
        mainList[i] = <ListItem key={i} value={props.MainList[i]} />

    }


       return (
        <Aux>
            <ul className = "mainList">{mainList}</ul>
            <button>Enter</button>
        </Aux>
       );
};

I put jsfiddle together:

Let me know if there is a better practice to toggle element in React;

Cheers!

  • List item toggle jsfiddle
// ----------------APP-------------------
class App extends React.Component {
  state = {
    mainList: [
      {
        label: 'GamePlay',
        id: 1,
      },
      {
        label: 'Visuals',
        id: 2,
      },
      {
        label: 'Audio',
        id: 3,
      },
      {
        label: 'Story',
        id: 4,
      },
    ],
  }

  render() {
    const { mainList } = this.state;

    return (
      <div>
        <List mainList={mainList} />
      </div>
    );
  }
}
// ----------------LIST-------------------
const List = ({ mainList }) => (
  <div>
    {mainList.map((listItem) => {
      const { label, id } = listItem;

      return (
        <ListItem key={id} id={id} label={label} />
      );
    })}
  </div>
);
// ----------------LIST-ITEM-------------------
class ListItem extends React.Component{
    state = {
      selected: false,
    }

    changeColor = () => {
      const { selected } = this.state;

      this.setState({selected: !selected})
    }

    render(){
      const { label, id } = this.props;
      const { selected } = this.state;

      return console.log(selected ,' id - ', id ) || (
        <button 
          className={selected ? 'green' : 'red'}
          onClick= {() => this.changeColor()}
        >
          {label}
        </button>
    )   
  }
} 
发布评论

评论列表(0)

  1. 暂无评论