리액트로 DB 없이 로컬 스토리지를 사용하여 간단한 게시판 만들어보기~! (feat. 라우터)
npx create-react-app react-boardnpm install react-router-domRoute의 component 속성 대신 render 속성을 사용한다. (참고)<Route path="/write" render={() => <Write addListItem={addListItem} />} />
<Route path="/write" render={(routeProps) => <Write addListItem={addListItem} {...routeProps} />} />
:(route parameter)/detail/글번호 형식의 주소를 주고 싶었는데, 이걸 경로 매개변수를 사용해서 동적으로 라우팅할 수 있다. (참고)<Route path="/update/:id" component={Update} />
props.match.params.id로 접근할 수 있다.history.push(`/detail/${id}`);
white-space에 pre-wrap 옵션을 주어 처리한다.(참고)<div style={{ whiteSpace: 'pre-wrap' }}>{content}</div>
// useInputs.js
import { useReducer } from 'react';
function reducer(state, action) {
return {
...state,
[action.name]: action.value, // e.target의 name과 value
};
}
export default function useInputs(initialState) {
const [state, dispatch] = useReducer(reducer, initialState);
const onChangeInput = (e) => {
dispatch(e.target);
};
return [state, onChangeInput];
}
// Write.js
...
import useInputs from '../useInputs';
...
const Write = ({ addListItem, id, history }) => {
const [state, onChangeInput] = useInputs({ title: '', content: '' });
const { title, content } = state;
...
return (
<div>
<h1>write</h1>
<form onSubmit={onSubmitForm}>
<input
ref={inputTitle}
placeholder="title"
name="title"
value={title}
onChange={onChangeInput}
/>
...
</div>
);
};
useReducer()useReducer() 쓰는 것도 연습할 겸 코드를 리팩토링해보았다.React.StrictMode에서 성능 테스트를 위해 의도적으로 개발 모드에서만 두 번 호출하는 함수들이 있다고 한다. StrictMode를 지웠더니 문제 해결! (참고)
안녕하세요!
제가 Spring boot와 react를 연동한 게시판을 만들고 있는데 블로그장님의 디자인이 이뻐서 css를 참고해서 만들었습니다!
제가 만든 게시판을 git이랑 블로그에 올려고 하는데 출처는 남길 예정입니다!
올리기 전에 허락을 먼저 구해야할 것 같아 이렇게 댓 작성합니다.
확인 후 답 주세요!