Frontend Development Guide
Learn how to develop, test, and maintain the frontend application for the DISCover Program template.
Overview
The frontend application is built with:
- React for UI components
- React Router for client-side routing
- styled-components for styling
- ESLint and Prettier for code formatting
- PropTypes for type checking
Development Environment
Code Editor Setup
- Install VSCode extensions:
- Prettier ESLint
- ESLint
- Configure VSCode settings:
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "rvest.vs-code-prettier-eslint"
}
Code Formatting Rules
Our ESLint and Prettier configurations enforce:
-
JSX Rules:
- Only
.jsxfiles can contain JSX - All
.jsxfiles must contain JSX - Empty files are allowed
- Only
-
Syntax Rules:
- Semicolons required
- Single quotes for strings
- 80 character line width
- 2-space indentation
-
Import Organization:
// 1. React imports
import React from "react";
import { useState } from "react";
// 2. Third-party modules
import styled from "styled-components";
import PropTypes from "prop-types";
// 3. Absolute paths from src/
import { useAuth } from "common/hooks/useAuth";
import Button from "common/components/Button";
// 4. Relative paths
import { StyledContainer } from "./styles";
Component Development
Component Structure
Create components following this template:
import React from "react";
import PropTypes from "prop-types";
import styled from "styled-components";
const StyledComponent = styled.div`
// styles
`;
const ComponentName = ({ prop1, prop2 }) => {
return <StyledComponent>{/* component content */}</StyledComponent>;
};
ComponentName.propTypes = {
prop1: PropTypes.string.isRequired,
prop2: PropTypes.number,
};
ComponentName.defaultProps = {
prop2: 0,
};
export default ComponentName;
Styling Guidelines
- Use styled-components for component styling:
const StyledButton = styled.button`
padding: 8px 16px;
border-radius: 4px;
// ...
`;
- Global styles go in
App.css - Use semantic HTML elements
- Maintain responsive design principles
State Management
- Local State:
const [count, setCount] = useState(0);
- Context for shared state:
// In context file
export const ThemeContext = React.createContext();
// In component
const theme = useContext(ThemeContext);
- Custom hooks for reusable logic:
const useFormInput = (initialValue) => {
const [value, setValue] = useState(initialValue);
return {
value,
onChange: (e) => setValue(e.target.value),
};
};
Development Process
Git Workflow
- Create feature branch:
git checkout -b feature/your-feature
- Review and stage changes:
# Recommended: Review changes in chunks with -p flag
git add -p # This lets you review each change individually
# Alternative: Stage all changes
git add .
The -p (or --patch) flag is highly recommended as it:
- Helps you review your own code before committing
- Allows you to stage specific parts of files
- Reduces accidental commits of debugging code
- Makes for cleaner, more focused commits
- Commit your changes:
git commit -m "feat: description of changes"
- Push and create PR:
git push origin feature/your-feature
Running the Application
Development server:
npm start
Run tests:
npm test
Build for production:
npm run build
Common Issues
-
Styling Issues
- Check styled-components syntax
- Verify component class names
- Inspect CSS specificity
-
Routing Problems
- Confirm route definitions
- Check navigation props
- Verify path parameters
-
State Management
- Debug React DevTools
- Check context providers
- Verify state updates
Best Practices
-
Component Guidelines:
- Keep components focused and single-purpose
- Use PropTypes for all props
- Implement error boundaries
- Memoize expensive computations
-
Performance Tips:
- Lazy load routes
- Use React.memo for pure components
- Optimize re-renders
- Monitor bundle size
-
Code Organization:
- Follow directory structure
- Use meaningful file names
- Keep related files together
- Document complex logic