• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 React 게시판 redux로 alert창 닫기 질문입니다.

redux로 alert창 닫기 질문입니다.

2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 글쓴이
  • #26576

    강병민
    참가자

    index.js

     

    import React from 'react'
    import ReactDOM from 'react-dom'
    import './index.css'
    import App from './App'
    import reportWebVitals from './reportWebVitals'
    import { BrowserRouter } from 'react-router-dom'
    import { Provider } from 'react-redux'
    import { createStore, combineReducers } from 'redux'

    // 닫기버튼 만들기
    let initAlert = true

    function reducer2(state = initAlert, action) {
      if (action.type === 'closeAlert') {
        state = false
        return state
      } else {
        return state
      }
    }

    // redux state
    let initVal = [
      { id: 0, name: '멋진신발', quan: 0 },
      { id: 1, name: '멋진신발1', quan: 1 },
      { id: 2, name: '멋진신발2', quan: 2 },
    ]

    function reducer(state = initVal, action) {
      //
      if (action.type === 'incQuan') {
     

        let copy = [...state]
        copy[0].quan++
        return copy
      } else if ((action.type = 'decQuan')) {
        let copy = [...state]
        copy[0].quan--
        return copy
      } else {
        return state
      }
    }

    let store = createStore(combineReducers({ reducer, reducer2 }))

    ReactDOM.render(
      <React.StrictMode>
        <BrowserRouter>
          <Provider store={store}>
            <App />
          </Provider>
        </BrowserRouter>
      </React.StrictMode>,
      document.getElementById('root')
    )

    reportWebVitals()

    /////////////////////////////////////////////////////////////////////////////

    Cart.js

    import React from 'react'
    import { Table, Button } from 'react-bootstrap'
    import { propTypes } from 'react-bootstrap/esm/Image'
    import { connect } from 'react-redux'
    import './App.css'
    import './Detail.scss'

    function Cart(props) {
      return (
        <div>
          <Table responsive>
            <tr>
              <th>상품id</th>
              <th>상품명</th>
              <th>수량</th>
              <th>변경</th>
            </tr>

           
            {props.state.map((a, i) => {
             
              return (
                <tr key={i}>
                  <td>{a.id}</td>
                  <td>{a.name}</td>
                  <td>{a.quan}</td>
                  <td>
                    <button
                      onClick={() => {
                       
                        props.dispatch({ type: 'incQuan' })
                      }}
                    >
                      +{' '}
                    </button>
                    <button
                      onClick={() => {
                        props.dispatch({ type: 'decQuan' })
                      }}
                    >
                      {' '}
                      -{' '}
                    </button>
                  </td>
                </tr>
              )
            })}
          </Table>
          {props.alertOpen === true ? (
            <div className="my-alert-yellow">
              <p>지금 구매하시면 신규할인 20%</p>
              <button
                onClick={() => {
                  props.dispatch({ type: 'closeAlert' })

                  console.log(props.alertOpen)
                }}
              >
                닫기
              </button>
            </div>
          ) : null}
        </div>
      )
    }

    function stateToProps(state) {

      return {
        state: state.reducer,
        alertOpen: state.reducer2,
      }
    }

    export default connect(stateToProps)(Cart)

    //export default Cart;

     

    질문 : 

    닫기 버튼을 누르면 모달창이 안닫히고 수량이 -1이 됩니다.

    그리고 props.alertOpen을 출력하면  계속 true가 뜹니다.

    #26608

    codingapple
    키 마스터

    알수없군요 reducer2안에서 console.log로 action같은걸 출력해봅시다 

    아니면 뒷 강의에 나오는 useDispatch 이걸 써봅시다

2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관, 개인정보처리방침
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠