mui TextField in React loosing focus everytime after typing one character

42 views Asked by At

I don't understand what's happening but everytime the textfield is loosing focus after i type a character....I have tried many things but nothing seems to work

This is my Search.jsx

import React, { useState, useEffect } from 'react';
import { TextField, InputAdornment, styled } from '@mui/material';
import { Search as SearchIcon } from '@mui/icons-material';
import { useDispatch, useSelector } from 'react-redux';
import { useLocation } from 'react-router-dom';

function Search() {
  const SearchContainer = styled('div')(({ theme }) => ({
    [theme.breakpoints.down('sm')]: {
      display: 'flex',
      justifyContent: 'center',
      width: '100%',
    },
  }));

  const SearchTextField = styled(TextField)(({ theme }) => ({
    color: theme.palette.mode === 'light' && 'black',
    filter: theme.palette.mode === 'light' && 'invert(1)',
    [theme.breakpoints.down('sm')]: {
      marginTop: '-10px',
      marginBottom: '10px',
    },
  }));

  const [query, setQuery] = useState('');

  const handleKeyPress = (e) => {
    if (e.key === 'Enter') {
      // Perform your search action here
      console.log('Performing search for:', query);
    }
  };

  return (
    <SearchContainer>
      <SearchTextField
        onKeyPress={handleKeyPress}
        value={query}
        onChange={(e) => setQuery(e.target.value)}
        variant="standard"
        InputProps={{
          startAdornment: (
            <InputAdornment position="start">
              <SearchIcon />
            </InputAdornment>
          ),
        }}
      />
    </SearchContainer>
  );
}

Search.whyDidYouRender = true;
export default Search;

I tried using callback and putting it into a different function but it's a simple onChange event to setQuery and i don't know where i am messing up also my movies component is rerendering itself fetching the same data mi8 have something to do with it

This is my Movies.jsx

import React, { useState, useEffect } from 'react';
import { Box, CircularProgress, useMediaQuery, Typography } from '@mui/material';
import { useSelector } from 'react-redux';

import { selectGenreOrCategory } from '../../features/currentGenreOrCategory';
import { useGetMoviesQuery } from '../../services/TMDB';
import MovieList from '../MovieList/MovieList';

function Movies() {
  const [page, setPage] = useState(1);
  const { genreIdOrCategoryName } = useSelector((state) => state.currentGenreOrCategory);
  const { data, error, isFetching } = useGetMoviesQuery({ genreIdOrCategoryName, page });

  if (isFetching) {
    return (
      <Box display="flex" justifyContent="center">
        <CircularProgress size="4rem" />
      </Box>
    );
  }

  if (!data.results.length) {
    return (
      <Box display="flex" alignItems="center" mt="20px">
        <Typography variant="h4">
          No movies that match that name.
          <br />
          Please search for something else.
        </Typography>
      </Box>
    );
  }

  if (error) return 'An error has occured.';

  return (
    <div>
      <MovieList movies={data} />
    </div>
  );
}

Movies.whyDidYouRender = true;
export default Movies;

wdyr shows the component being rerendered

0

There are 0 answers