[Javascript] Toggle Between Grid and List View in React

This article will walk you through the process of changing the layout view from grid to list and back. Note: This article is a follow up from, Render Fetched Data from a GraphQL API in React.

What we’re building.

So far, we have rendered the Rick and Morty API data with a grid layout using the Bootstrap CSS framework and Sass.

Declaring State

The first step is to define the state and its default value in the App.js. Here, toggleViewand setToggleViewrefer to the state value and updater function returned on invoking useState with some initialState. Use a boolean to toggle between the grid and list view. Here, the default value (true) means it’s in grid view, and if it’s false, it will be in list view.

const [toggleView, setToggleView] = useState(true);

Create a button that allows you to toggle between the grid and list view. Add the onClick() property with a console.log() to test that the functionality works as expected.

<div className="trigger-button" onClick={() => { console.log('clicked') }}>

Once you have the click functionality working, swap out the console.log for the updater function and pass in the toggleView state as the parameter:

<div className="trigger-button" onClick={() => setToggleView(!toggleView)}>

We will be using a ternary operator to implement the functionality of the toggle between the grid view and list view. If the default state is true, meaning the grid view is rendered, a list icon will show. If the state is false, then the list view is rendered and the grid icon will show.

Bootstrap Icons

First, install Bootstrap Icons:

$ npm install bootstrap-icons

Then, add it to the custom.scss file, just below the Bootstrap import:

@import "../node_modules/bootstrap/scss/bootstrap.scss";
@import "../node_modules/bootstrap-icons/font/bootstrap-icons.css";

Next, write the ternary operator logic:

{toggleView ? "list" : "grid"}

Search for the grid and list icons:

Swap out the text in the ternary operator to conditionally render the icons we will be using. Be sure to wrap each code snippet in parentheses.

{toggleView 
? (
    <i className="bi bi-list"></i>
  ) : (
    <i className="bi bi-grid"></i>
  )
} 

We want this button to be fixed to the top right corner of the browser window, since we’ve already given the button a class name, trigger-button , use fixed positioning to remove the button from the normal flow and offset it 30 pixels from the right.

Make the icon bigger by adjusting the font-size.

.trigger-button {
    position: fixed;
    right: 30px;
}

.trigger-button i {
  font-size: 2em;
}

Finally, modify the className="row" to include the ternary that toggles between the row which renders the characters in grid layout and the col which renders the characters in list layout.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

<div className={toggleView ? "row" : "col"}>

Passing Down Props

The styling for the list view is still not correct. To get this to look like the mockup GIF above, pass the toggleView prop down to the CharacterInfo component:

<CharacterInfo 
  key={character.id} 
  character={character} 
  toggleView={toggleView} 
/>

The CharacterInfo functional component can then take the toggleView prop as a parameter.

const CharacterInfo = ({ character, toggleView }) => {}

Lucky for us, we can use the same ternary operator logic to conditionally render different layouts for the grid and the list.

{toggleView ? "grid" : "list"}

Remember, we want to see the list icon when we are in grid view and the grid icon when we are in list view. Therefore, we need our default toggleView state to be the grid:

 const CharacterInfo = ({ character, toggleView }) => {
    return(
        <div className="col mb-5">
            {toggleView ? (
                <div className="card">
                    <img src={character.image} className="card-img-top" alt={character.name}></img>
                    <div className="card-body" style={{ width: "18rem" }}>
                        <h4 className="card-title">{character.name}</h4>
                        <p className="card-text">
                            {character.gender}
                            <span className="float-end">{character.species}</span>
                        </p>
                    </div>
                </div>
            ) : (
                <div className="list-view">
                    <div className="row">
                        <div className="col-2">
                            <img src={character.image} className="rounded float-start" alt={character.name}></img>
                        </div>
                        <div className="col-10">
                            <h2 className="card-title">{character.name}</h2>
                            <h3 className="card-text">{character.gender}</h3>
                            <h3 className="card-text">{character.species}</h3>
                        </div>
                    </div>
                </div>
            )}

        </div>
    )
}

export default CharacterInfo

The grid layout is what was originally created in the previous article. Now, we need to create a layout for the list view. Let’s start by creating a new CSS class, list-view which duplicates the box-shadow and border style of the card element.

.list-view {
  padding: 5px;
  box-shadow: 5px 5px 1px 1px $softBlack;
  border: 2px solid $softBlack;
  border-radius: 8px;
}

Next, we will use Bootstrap’s grid system to create two columns for each character — one with the image and the other with the text content. You can copy-paste this snippet from the Bootstrap documentation.

<div className="list-view">
  <div className="row">
    <div className="col-2"></div>
    <div className="col-10"></div>
  </div>
</div>

Inside the col-2 element we will place the image and give it a class name rounded float-start .

<div className="col-2">
  <img src={character.image} className="rounded float-start" alt={character.name}></img>
</div>

Give the image a width of 200px to overwrite the the Bootstrap classes rounded float-start:

.list-view img {
  width: 200px;
}

Inside the col-10 element, we will place the text content — namegender and species. You can also add the character’s statustype and origin.

<div className="col-10">
    <h2 className="card-title">
        Name: {character.name},
        <small className="text-body-secondary"> ({character.gender})</small>
    </h2>
    <p className="h3">
        Species: {character.species}
    </p>
    <p className="h3">Type: {character.type === "" ? "Unknown" : `${character.type}`}</p>
    <p className="h3">Origin: {character.origin.name === "unknown" ? "Unknown" : `${character.origin.name}`}</p>
</div>

Some of the properties have empty string values or unknown values. Use a ternary operator to conditionally render the text you’d like to show instead.

Conclusion

The previous steps walk through the process of toggling the layout view between grid and list using conditional rendering in React, a few lines of custom CSS and Bootstrap classes.

 
 

[출처] https://medium.com/@layne_celeste/toggle-between-grid-and-list-view-in-react-731df62b829e

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
27 [wordpress, 워드프레스] 워드프레스로 쉽게 랜딩 페이지 만들기 (feat. 엘리멘터, GeneratePress, Divi) file 졸리운_곰 2024.12.20 545
26 [wordpress, 워드프레스] WordPress 페이지또는 글에 별도 CSS와 JavaScript 추가하기 file 졸리운_곰 2024.12.19 354
25 [wordpress 플러그인] 코스모스팜. 회원관리 1.메뉴에 로그인 넣기 file 졸리운_곰 2024.08.25 513
24 [wordpress 플러그인] WP-Members로 회원제 사이트 만들기 file 졸리운_곰 2024.08.25 749
23 [php worldpress] 워드프레스 새 서버 이전시 페이지 안나오는 문제 : How to Rewrite URLs with mod_rewrite for Apache on Ubuntu 20.04 file 졸리운_곰 2024.08.07 774
22 [php worldpress] PHP query to SQL server database (wordpress) 졸리운_곰 2024.07.28 503
21 [php worldpress] [위 에]wordpress 사용자 암호 화 원리 및 알고리즘 분석 졸리운_곰 2022.04.11 377
20 [wordpress] WPForms 워드프레스 폼빌더 사용법 file 졸리운_곰 2021.04.25 1715
19 [wordpress][워드프레스] 워드프레스에서 wpdb를 사용한 CRUD 작업 예 졸리운_곰 2021.04.15 356
18 워드프레스에서 wpdb를 사용한 CRUD 작업 예 졸리운_곰 2021.01.12 459
17 워드프레스 – CRUD file 졸리운_곰 2021.01.02 500
16 워드프레스 데이터베이스 들여다보기. file 졸리운_곰 2020.08.04 672
15 워드프레스 플러그인과 테마 비교 - 사이트별 플러그인 만들기 졸리운_곰 2020.04.21 501
14 워드프레스에서 js 스크립트 파일과 스타일시트를 올바르게 로드하는 방법 졸리운_곰 2020.04.21 735
13 워드프레스 플러그인 만들기 file 졸리운_곰 2020.04.21 499
12 워드프레스 숏코드: 완벽 가이드 file 졸리운_곰 2020.04.21 513
11 워드프레스 페이지 분석 file 졸리운_곰 2019.11.19 515
10 내가 본 워드프레스 핵심 구조 및 기능 (Wordpress Architecture and Function) file 졸리운_곰 2019.11.19 452
9 Embedding three.js in WordPress 워드프레스에서 three.js 사용 졸리운_곰 2019.11.08 517
8 How to Super Charge your WordPress with Microservices 워드프레스 마이크로서비스 file 졸리운_곰 2019.11.03 373
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED