본문 바로가기

반응형

Programming

(317)
React강좌3 - Redux4. Store 예시 어플리케이션의 현재 상태를 지니고 있음 store역할 dispatch(action) : 새 상태를 현 상태에 갈아끼움 getState() : 현재 상태를 반환 subscribe(listener): 상태가 바뀔때마다 실행할 콜백함수(listener) 등록 replaceReducer(nextReducer) 추가로, redux-devtools-extension을 설치하고, 크롬에서 Redux DevTools를 설치한다. yarn add redux-devtools-extension --dev 1. src/index.js store예시 import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from '..
React강좌3 - Redux3. Reducers 변화를 일으키는 함수 순수해야함 비동기 X 전달받은 인수변경 X 동일한 인수 = 동일한 결과 이전상태와 액션을 받아서 다음상태를 반환한다. (이전상태 변경이 아님) (previous State, Action) => newState SRC/Reducers 폴더안에 아래 3 파일을 만든다. 1. Counter.js (숫자 계산) 2. ui.js (색상 변경) 3. index.js (Reducer 통합) 1. Counter.js import * as types from '../Actions/ActionTypes'; //초기상태 정하기 const initialState = { number: 0, //최초 초기상태 정의 }; export default function counter(state = initialSt..
React강좌3 - Redux2. Action작성 1. 값 증가 액션 2. 값 감소 액션 3. 색 변경 액션 SRC/Actions폴더에 아래 2개의 파일 생성 1. ActionTypes.js export const INCREMENT = "INCREMENT"; export const DECREMENT = "DECREMENT"; export const SET_COLOR = "SET_COLOR"; 2. index.js // 액션 생성자 //import { INCREMENT, DECREMENT, SET_COLOR } from './ActionTypes'; import * as types from './ActionTypes'; //위 구문과 동일한 의미 export function increment() { return { type: types.INCREMENT ..
React강좌3 - Redux 1. Redux 설치 및 기본 컴포넌트 작성 1. Redux 설치 npm install --save redux react-redux 2. 3개의 컴포넌트 작성 (src/components 폴더에 작성) Value.js : 멍청이 Control.js : 멍청이 Counter.js : 똑똑이 (1) Value.js import React, { Component, PropTypes } from 'react'; const defaultProps = { number: -1 }; class Value extends Component { render() { return ( {this.props.number} ); } } Value.defaultProps = defaultProps; export default Value; (2) Control.js import ..
React에서 Highchart 사용하기 기초 예제 오늘은 React에서 Highchart 사용하여 아래와 같은 BAR 차트를 한번 그려보려고 합니다. 1. 설치 우선 create-react-app으로 리액트 환경을 구축하고, Highcharts 와 highcharts-react-official을 설치해 줍니다. create-react-app .// '.' 을 입력하면 현재 폴더에 리액트를 설치합니다. npm install highcharts --save npm install highcharts-react-official --save 2. High.js (Highchart 컴포넌트) 작성 이번 예제에서는 App.js에서 데이터를 props로 전달해주고, Highchart 컴포넌트에서 이를 받아 그래프를 그리도록 구성하겠습니다. 따라서 그래프를 그리는 Hi..
React강좌2, 전화번호부 - 0.기본 파일 1. App.js import React from 'react'; import Contact from './Contact'; import './App.css'; function App() { return ( ); } export default App; 2. Contact.js import React, { Component } from 'react'; import ContactInfo from './ContactInfo'; class Contact extends Component { constructor(props) { super(props); this.state = { selectedKey: -1, keyword: '', contactData: [{ name: 'Abet', phone: '010-000-..
React강좌2, 전화번호부 - 6.LocalStorage에 저장하기 LocalStorage에 저장하여 update되어도 데이터가 초기상태로 돌아가지 않도록 하는 기능을 구현하겠습니다. componentWillMount API와 componentDidUpdate API만 구현해주면 됩니다. Contact.js import React, { Component } from 'react'; import ContactInfo from './ContactInfo'; import ContactDetails from './ContactDetails'; import ContactCreate from './ContactCreate'; class Contact extends Component { constructor(props) { super(props); this.state = { sele..
React강좌2, 전화번호부 - 5.Update 기능 Update기능은 좀 어렵습니다. 구현해야 할 내용으로는 ContactDetails 컴포넌트에서 1. Edit 버튼을 만듭니다. 이 버튼은 Edit모드와 비 Edit모드사이를 토글할 수 있도록 합니다. 2. 토글 함수를 선언해줍니다. isEdit 값이 false이면 Edit 폼에 name과 phone이 변경 가능하도록 데이터를 보여주며, true이면(수정 완료버튼 클릭) name과 phone 값을 상위요소인 Contact 컴포넌트로 전달해줍니다. 전달함수는 별도로 3번에서 만듭니다. 3. handleEdit 함수를 선언합니다. 이 함수에서 isEdit가 true일 때 name과 phone 을 Contact 컴포넌트로 전달해줍니다. 4. Edit 폼을 만들어줍니다. 그리고 isEdit 값에 따라 표시되는 ..

반응형