English 中文(简体)
How can I show or hide a div as i toggle an element from a list?
原标题:

I have a list of elements where each element is a Game and as I toggle I dispatch a action to my slice where I want to affect the value o isDivOpen but just for the element (Game) I clicked on.

I created the following slice:

import { createSlice, createAsyncThunk } from  @reduxjs/toolkit ;
import axios from  axios ;

const initialState = {
  games: [],
  isLoading: false,
  isDivOpen: true,
};


export const gameSlice = createSlice({
  name:  games ,
  initialState,
  reducers: {
    toggleSeeMore(state, action) {
      const gameId = action.payload;
      console.log(gameId);
    },
  },
  
})

export const { toggleSeeMore } = gameSlice.actions;

export default gameSlice.reducer;

And the following is my component:

import React from  react ;
import ExpandMoreIcon from  @mui/icons-material/ExpandMore ;
import  ../../styles/components/Games/Game.css ;
import { useSelector, useDispatch  } from  react-redux ;
import { toggleSeeMore } from  ../../slices/gameSlice ;


function Game({ game }) {
  const { name, description, employees, hours, photo } = game;
  const isDivOpen = useSelector(state => state.games.isDivOpen);
  

  const dispatch = useDispatch()


  const toggleMenu = () => {
    const gameId = game._id
    dispatch(toggleSeeMore(gameId))
  }

  return (
    <div className= game_card >
      <div className="game_heading">
        <div className="image_container">
          <img src={`http://localhost:5000/uploads/${photo}`} alt="no photo" />
        </div>
        <h1>{name}</h1>
      </div>
      <div className="game_content">
        <p className="description">{description}</p>
        <div className="game_hours">
          {formattedTimes.map((time, index) => (
            <div key={index}>{time}</div>
          ))}
        </div>
      </div>
      <button onClick={toggleMenu}>See More<ExpandMoreIcon/></button>
      <div className={`employees ${isDivOpen ?  show  :  hide }`}>
        <p>Employees</p>
        {employees.map((employee, index) => (
          <div key={index}>{employee}</div>
        ))}
      </div>
    </div>
  );
}

export default Game;

I am receiving the correct id of the game im clicking on, but i dont know how to change the value of isDivOpen just for that game.

问题回答

This could be more simple than it is currently,

Reducers could be helpful for large and complex state management but for the thing that show or hide is not really necessary

You can delete the isDivOpen in redux state, then you can declare an useState for that in your component and hide or show based on that state... it will affect item individually

  const [showMenu, setShowMenu] = useState(true);

  // Another logic

  const toggleMenu = () => {
    setShowMenu(!showMenu)
  }

That being said if you want to still show or hide each one using the reducer you has to basically change your structure and each game has to have the value related to if it is open or not

Hope it helps





相关问题
How to use one react app into another react app?

I have two react apps, parent app and child app. and child app have an hash router. I have build the child app using npm run build, It creates build folder. That build folder moved into inside the ...

how to get selected value in Ant Design

I want to print the selected value, and below is my option list: const vesselName = [ { value: 0 , label: ALBIDDA , }, { value: 1 , label: ALRUMEILA , }, { value: 2 ,...

How to add a <br> tag in reactjs between two strings?

I am using react. I want to add a line break <br> between strings No results and Please try another search term. . I have tried No results.<br>Please try another search term. but ...

热门标签