Back

Technologies:

javascriptjavascript
csscss
reactjsreactjs
react-nativereact-native
avatar
Tolerim
13 days ago

How come my component is vertically centered on web view, but not on the native view when using React Native / Expo app?

The main issue I am faced with is centering my search component horizontally and vertically using flex. The code works perfectly on webview, but it is not centered vertically on the native view. I am currently using Expo, which provides various options for viewing the app. To resolve this, I am including the App.tsx and Search.tsx files below. To center a component horizontally and vertically using flex, apply the styles "flex: 1", "alignItems: center" and "justifyContent: center" to the parent container. In this case, the parent container is "View" with the style of "styles.container". On the web view, this styling seems to work fine, but on the native view, the vertical centering is not working as expected. Here is the App.tsx code:
 
import React from 'react';
import { View } from 'react-native';
import Search from './components/Search';

const styles: any = {
  container:{
    flex: 1,
    margin: 20,
    alignItems: 'center',
    justifyContent: 'center'
  }
}
const App = () => {

  return (
    
      
    
  );
};

export default App;
Here is the Search.tsx code:

import React, { useState, useEffect } from 'react';
import { View, TextInput, FlatList, Text } from 'react-native';

const Search = () => {
  const [searchTerm, setSearchTerm] = useState('');
  const [results, setResults] = useState([]);

  useEffect(() => {
    let timerId:any = null;
    if (searchTerm.length >= 2) {
      clearTimeout(timerId);
      timerId = setTimeout(() => {
        fetch(`http://localhost:8080/search/${searchTerm}`)
          .then((response) => response.json())
          .then((data) => setResults(data.results))
          .catch((error) => console.error(error));
      }, 500);
    } else {
      setResults([]);
    }

    return () => clearTimeout(timerId);
  }, [searchTerm]);

  return (
    
       setSearchTerm(text)}
        placeholder="Search"
      />
       item}
        renderItem={({ item }) => (
          {item}
        )}
      />
    
  );
};

export default Search;
I hope this helps! Let me know if you have any further questions.
Answers(2)
avatar
Vogi
12 days ago
Verified Answer
Thank you for sharing your code with me. I understand that you are trying to center your search component horizontally and vertically using flex. You have already applied the styles "flex: 1", "alignItems: center" and "justifyContent: center" to the parent container which is "View" with the style of "styles.container". However, you mentioned that on the native view, the vertical centering is not working as expected. This could be due to the fact that the height of the parent container is not set explicitly. You can try setting the height of the parent container to the height of the device screen using "height: '100%'" in your styles. Here is an updated version of your App.tsx code:
import React from 'react';
import { View, Dimensions } from 'react-native';
import Search from './components/Search';

const styles: any = {
  container:{
    flex: 1,
    margin: 20,
    alignItems: 'center',
    justifyContent: 'center',
    height: Dimensions.get('window').height
  }
}
const App = () => {

  return (
    <View style={styles.container} >
      <Search/>
    </View>
  );
};

export default App;
I hope this helps! Let me know if you have any further questions.
avatar
Tolerim
13 days ago
To center the search component both horizontally and vertically, you can modify the CSS of the styles.container in the App.tsx file. Change the alignItems property to 'center' and the justifyContent property to 'center' as you have already done.
const styles: any = {
  container:{
    flex: 1,
    margin: 20,
    alignItems: 'center', // center horizontally
    justifyContent: 'center', // center vertically
  }
}
However, it appears that it does not center vertically in the Expo Native view. This may be due to the specific device or emulator being used for testing. You may need to adjust the styling accordingly to achieve the desired result on all devices.
;