Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/migrate page ,index and TraceIdSearch to functional component #2673

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Mubashirshariq
Copy link

@Mubashirshariq Mubashirshariq commented Feb 18, 2025

Which problem is this PR solving?

Migrating Class components to functional components
/attempt #2610

Description of the changes

migrate page,index and traceidsearch

How was this change tested?

Checklist

@Mubashirshariq Mubashirshariq requested a review from a team as a code owner February 18, 2025 19:37
@Mubashirshariq Mubashirshariq requested review from joe-elliott and removed request for a team February 18, 2025 19:37
@Mubashirshariq Mubashirshariq force-pushed the fix/change-class-based-cops branch from 692e8e7 to e834cce Compare February 18, 2025 19:42
@yurishkuro yurishkuro added the changelog:bugfix-or-minor-feature 🐞 Bug fixes, Minor Improvements label Feb 18, 2025
Copy link

codecov bot commented Feb 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.59%. Comparing base (caed0b4) to head (e834cce).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2673      +/-   ##
==========================================
- Coverage   96.59%   96.59%   -0.01%     
==========================================
  Files         256      256              
  Lines        7727     7718       -9     
  Branches     2002     2024      +22     
==========================================
- Hits         7464     7455       -9     
  Misses        263      263              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

componentDidMount() {
const { pathname, search } = this.props;
export const PageImpl: React.FC<TProps> = ({ children, embedded, pathname, search }) => {
useEffect(() => {
trackPageView(pathname, search);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously this was called conditionally. What happens now?

if (pathname !== nextPathname || search !== nextSearch) {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yurishkuro The conditional check if (pathname !== nextPathname || search !== nextSearch) was removed because useEffect's dependency array [pathname, search] handles this optimization automatically. The effect will only run when either dependency changes, making the manual comparison redundant. This achieves the same optimization with cleaner code.

@Mubashirshariq Mubashirshariq changed the title Feact/migrate page ,index and TraceIdSearch to functional component Feat/migrate page ,index and TraceIdSearch to functional component Feb 21, 2025
}
// Initialize API and process scripts
JaegerAPI.apiRoot = DEFAULT_API_ROOT;
processScripts();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this ok to call this function unconditionally on importing index.jsx? Isn't there some lifecycle thing in functional components when it can be called? Is there an official recommendation for functions like this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i thought this is just a configuration step,which we can call at module level,but the processScripts interacts with DOM so it need to be in useEffect.updated it with useEffect so that it will run whenver the component mounts

Comment on lines 81 to 84
useEffect(() => {
JaegerAPI.apiRoot = DEFAULT_API_ROOT;
processScripts();
}, []);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useEffect runs after the component mounts initially, while constructors are executed before the component mounts.
A better alternative would be to use a custom hook that uses a ref/state to keep track of the functional. Read a nice article here that explains why functional components lack this constructor alternative and what should be the workaround.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but i think here it needs to be inside the useEffect since processScripts() directly interacts with DOM,so it should be rendered every time the component is mounted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:bugfix-or-minor-feature 🐞 Bug fixes, Minor Improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants