2022-01-27 19:00:36 +05:30
|
|
|
# コードベースのベストプラクティス
|
2021-09-22 07:28:36 -07:00
|
|
|
|
2022-01-27 19:00:36 +05:30
|
|
|
## 一般的な JavaScript
|
2021-09-22 07:28:36 -07:00
|
|
|
|
2022-02-19 20:15:42 +05:30
|
|
|
ほとんどの場合、[リンター](how-to-setup-freecodecamp-locally.md#以下の手順に従って、開発環境を準備してください。) は、コードベースの好ましいプラクティスに反するフォーマットを警告します。
|
2021-09-22 07:28:36 -07:00
|
|
|
|
2022-02-07 19:23:40 +05:30
|
|
|
クラスベースのコンポーネントよりも関数コンポーネントの使用を推奨します。
|
2021-09-22 07:28:36 -07:00
|
|
|
|
2022-01-27 19:00:36 +05:30
|
|
|
## 特定の TypeScript
|
2021-09-22 07:28:36 -07:00
|
|
|
|
2022-01-27 19:00:36 +05:30
|
|
|
### JavaScript ファイルを TypeScript に移行する
|
2021-09-22 07:28:36 -07:00
|
|
|
|
2022-01-27 19:00:36 +05:30
|
|
|
#### Git のファイル履歴を保持する
|
2021-09-22 07:28:36 -07:00
|
|
|
|
2022-01-27 19:00:36 +05:30
|
|
|
ファイル形式を `<filename>.js` から `<filename>.ts` (もしくは `.tsx`) へ変更すると、元のファイルが削除され新しいファイルが作成される場合があります。それ以外の場合は、Git においてファイル名が変更されます。 ファイルの履歴を保存できるのが理想です。
|
2021-09-22 07:28:36 -07:00
|
|
|
|
2022-01-27 19:00:36 +05:30
|
|
|
そのための最善策は次のとおりです。
|
2021-09-22 07:28:36 -07:00
|
|
|
|
2022-01-27 19:00:36 +05:30
|
|
|
1. ファイル名を変更する
|
|
|
|
2. フラグ `--no-verify` でコミットして、Husky がリントエラーについて不平を言うことを防ぐ
|
|
|
|
3. 別のコミットで、移行のために TypeScript にリファクタリングする
|
2021-09-22 07:28:36 -07:00
|
|
|
|
2022-02-07 19:23:40 +05:30
|
|
|
> [!NOTE] VScode 等のエディターは、ファイルが削除され新しいファイルが作成されたことを表示する可能性があります。 `git add .` に CLI を使用すると、VSCode はファイル名が変更されたものとしてステージに表示します。
|
2021-09-22 07:28:36 -07:00
|
|
|
|
2022-01-27 19:00:36 +05:30
|
|
|
### 命名規則
|
2021-09-22 07:28:36 -07:00
|
|
|
|
2022-01-27 19:00:36 +05:30
|
|
|
#### インターフェースと型
|
2021-09-22 07:28:36 -07:00
|
|
|
|
2022-01-27 19:00:36 +05:30
|
|
|
ほとんどの場合、型宣言にインターフェース宣言を使用することを推奨します。
|
2021-09-22 07:28:36 -07:00
|
|
|
|
2022-01-27 19:00:36 +05:30
|
|
|
React コンポーネントプロパティ - サフィックスは `Props`
|
2021-09-22 07:28:36 -07:00
|
|
|
|
|
|
|
```typescript
|
|
|
|
interface MyComponentProps {}
|
|
|
|
// type MyComponentProps = {};
|
|
|
|
const MyComponent = (props: MyComponentProps) => {};
|
|
|
|
```
|
|
|
|
|
2022-01-27 19:00:36 +05:30
|
|
|
React ステートフルコンポーネント - サフィックスは `State`
|
2021-09-22 07:28:36 -07:00
|
|
|
|
|
|
|
```typescript
|
|
|
|
interface MyComponentState {}
|
|
|
|
// type MyComponentState = {};
|
|
|
|
class MyComponent extends Component<MyComponentProps, MyComponentState> {}
|
|
|
|
```
|
|
|
|
|
2022-01-27 19:00:36 +05:30
|
|
|
デフォルト - PascalCase 内のオブジェクト名
|
2021-09-22 07:28:36 -07:00
|
|
|
|
|
|
|
```typescript
|
|
|
|
interface MyObject {}
|
|
|
|
// type MyObject = {};
|
|
|
|
const myObject: MyObject = {};
|
|
|
|
```
|
|
|
|
|
|
|
|
<!-- #### Redux Actions -->
|
|
|
|
|
|
|
|
<!-- TODO: Once refactored to TS, showcase naming convention for Reducers/Actions and how to type dispatch funcs -->
|
|
|
|
|
|
|
|
## Redux
|
|
|
|
|
2022-01-27 19:00:36 +05:30
|
|
|
### Action 定義
|
2021-09-22 07:28:36 -07:00
|
|
|
|
|
|
|
```typescript
|
|
|
|
enum AppActionTypes = {
|
|
|
|
actionFunction = 'actionFunction'
|
|
|
|
}
|
|
|
|
|
|
|
|
export const actionFunction = (
|
|
|
|
arg: Arg
|
|
|
|
): ReducerPayload<AppActionTypes.actionFunction> => ({
|
|
|
|
type: AppActionTypes.actionFunction,
|
|
|
|
payload: arg
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2022-01-27 19:00:36 +05:30
|
|
|
### Reduce の方法
|
2021-09-22 07:28:36 -07:00
|
|
|
|
|
|
|
```typescript
|
|
|
|
// Base reducer action without payload
|
|
|
|
type ReducerBase<T> = { type: T };
|
|
|
|
// Logic for handling optional payloads
|
|
|
|
type ReducerPayload<T extends AppActionTypes> =
|
|
|
|
T extends AppActionTypes.actionFunction
|
|
|
|
? ReducerBase<T> & {
|
|
|
|
payload: AppState['property'];
|
|
|
|
}
|
|
|
|
: ReducerBase<T>;
|
|
|
|
|
|
|
|
// Switch reducer exported to Redux combineReducers
|
|
|
|
export const reducer = (
|
|
|
|
state: AppState = initialState,
|
|
|
|
action: ReducerPayload<AppActionTypes>
|
|
|
|
): AppState => {
|
|
|
|
switch (action.type) {
|
|
|
|
case AppActionTypes.actionFunction:
|
|
|
|
return { ...state, property: action.payload };
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
2022-01-27 19:00:36 +05:30
|
|
|
### Dispatch の方法
|
2021-09-22 07:28:36 -07:00
|
|
|
|
2022-01-27 19:00:36 +05:30
|
|
|
コンポーネント内で、必要なアクションとセレクターをインポートします。
|
2021-09-22 07:28:36 -07:00
|
|
|
|
|
|
|
```tsx
|
|
|
|
// Add type definition
|
|
|
|
interface MyComponentProps {
|
|
|
|
actionFunction: typeof actionFunction;
|
|
|
|
}
|
|
|
|
// Connect to Redux store
|
|
|
|
const mapDispatchToProps = {
|
|
|
|
actionFunction
|
|
|
|
};
|
|
|
|
// Example React Component connected to store
|
|
|
|
const MyComponent = ({ actionFunction }: MyComponentProps): JSX.Element => {
|
|
|
|
const handleClick = () => {
|
|
|
|
// Dispatch function
|
|
|
|
actionFunction();
|
|
|
|
};
|
|
|
|
return <button onClick={handleClick}>freeCodeCamp is awesome!</button>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default connect(null, mapDispatchToProps)(MyComponent);
|
|
|
|
```
|
|
|
|
|
|
|
|
<!-- ### Redux Types File -->
|
|
|
|
<!-- The types associated with the Redux store state are located in `client/src/redux/types.ts`... -->
|
|
|
|
|
2022-01-27 19:00:36 +05:30
|
|
|
## その他資料
|
2021-09-22 07:28:36 -07:00
|
|
|
|
2022-01-27 19:00:36 +05:30
|
|
|
- [TypeScript ドキュメント](https://www.typescriptlang.org/docs/)
|
|
|
|
- [React CheatSheet 付き TypeScript](https://github.com/typescript-cheatsheets/react#readme)
|