Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

React Native Tips for Writing Clean Code

Writing clean code in React Native is essential for maintainability, scalability, and collaboration in any development project. Here are some key practices and tips to ensure your React Native code remains clean and efficient:

1.Keep Components Small:

In React Native, it’s crucial to keep components small and focused on a single responsibility. This approach enhances readability, maintainability, and testability.

Example:

Instead of creating a large component that handles multiple tasks, break it down into smaller components. For instance, if you have a UserProfile component, you can split it into smaller components like UserAvatar, UserInfo, and UserActions.

2.Use PropTypes for Validation:

Using PropTypes in React Native is a best practice for type-checking the props passed to your components. This helps prevent runtime errors and documents component API.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import PropTypes from 'prop-types';
const UserProfile = ({ name, age }) => (
// Component logic
);
UserProfile.propTypes = {
name: PropTypes.string.isRequired,
age: PropTypes.number,
};
import PropTypes from 'prop-types'; const UserProfile = ({ name, age }) => ( // Component logic ); UserProfile.propTypes = { name: PropTypes.string.isRequired, age: PropTypes.number, };
import PropTypes from 'prop-types';

const UserProfile = ({ name, age }) => (
  // Component logic
);

UserProfile.propTypes = {
  name: PropTypes.string.isRequired,
  age: PropTypes.number,
};

3.Functional Over Class Components:

functional components whenever possible. They are generally less complex, easier to read, and more efficient compared to class components. With the advent of hooks, functional components can effectively handle state and lifecycle features, traditionally managed by class components​​.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import React, { useState } from 'react';
const Counter = () => {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
};
import React, { useState } from 'react'; const Counter = () => { const [count, setCount] = useState(0); return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}> Click me </button> </div> ); };
import React, { useState } from 'react';

const Counter = () => {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
};

4.Avoid Inline Styles:

To maintain a clear separation of concerns, it’s advised to use external stylesheets or CSS modules instead of inline styles. This approach aids in keeping the presentation layer separate from the logic, making your code more organized and maintainable​​.

Example:

Instead of using inline styles:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<View style={{ padding: 20, borderColor: 'black', borderWidth: 1 }} />
<View style={{ padding: 20, borderColor: 'black', borderWidth: 1 }} />
<View style={{ padding: 20, borderColor: 'black', borderWidth: 1 }} />

Use StyleSheet:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import { StyleSheet, View } from 'react-native';
const styles = StyleSheet.create({
container: {
padding: 20,
borderColor: 'black',
borderWidth: 1,
},
});
<View style={styles.container} />
import { StyleSheet, View } from 'react-native'; const styles = StyleSheet.create({ container: { padding: 20, borderColor: 'black', borderWidth: 1, }, }); <View style={styles.container} />
import { StyleSheet, View } from 'react-native';

const styles = StyleSheet.create({
  container: {
    padding: 20,
    borderColor: 'black',
    borderWidth: 1,
  },
});

<View style={styles.container} />

5.Use Arrow Functions for Event Handlers:

Arrow functions in JavaScript ES6 make your code more concise and improve readability. They automatically bind to the context where they are defined, simplifying the handling of ‘this’ keyword​​.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const MyComponent = () => {
const handleClick = () => {
// handle the click
};
return <Button onPress={handleClick} title="Click me" />;
};
const MyComponent = () => { const handleClick = () => { // handle the click }; return <Button onPress={handleClick} title="Click me" />; };
const MyComponent = () => {
  const handleClick = () => {
    // handle the click
  };

  return <Button onPress={handleClick} title="Click me" />;
};

6.Implement Stateless Components:

Whenever possible, use stateless components. They are simpler, more predictable, and generally faster as they do not manage any internal state. This makes them ideal for components that purely depend on inputs (props) to render​​.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const Greeting = ({ name }) => <Text>Hello, {name}!</Text>;
const Greeting = ({ name }) => <Text>Hello, {name}!</Text>;
const Greeting = ({ name }) => <Text>Hello, {name}!</Text>;

7. Utilize the Spread Operator for Props:

The spread operator (…) is a useful feature in JavaScript for working with arrays and objects. It allows for more concise code and can be particularly helpful for propagating props in React Native components​​.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const MyComponent = ({ title, ...rest }) => (
<View {...rest}>
<Text>{title}</Text>
</View>
);
const MyComponent = ({ title, ...rest }) => ( <View {...rest}> <Text>{title}</Text> </View> );
const MyComponent = ({ title, ...rest }) => (
  <View {...rest}>
    <Text>{title}</Text>
  </View>
);

8.Adopt TypeScript for Type Safety:

TypeScript provides a type-checked system, reducing the need for prop validation libraries and enhancing the reliability of your code. It’s increasingly popular and helps in tracking the types of parameters and return types in your code​​.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
interface UserProps {
name: string;
age: number;
}
const User: React.FC<UserProps> = ({ name, age }) => (
<Text>{name} is {age} years old</Text>
);
interface UserProps { name: string; age: number; } const User: React.FC<UserProps> = ({ name, age }) => ( <Text>{name} is {age} years old</Text> );
interface UserProps {
  name: string;
  age: number;
}

const User: React.FC<UserProps> = ({ name, age }) => (
  <Text>{name} is {age} years old</Text>
);

9.Organize and Sort Imports:

Structuring imports in a logical manner (like grouping third-party libraries, company libraries, and local imports) and sorting them alphabetically can enhance the readability and maintainability of your code​​.

Example:

Before organizing:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import { View } from 'react-native';
import MyComponent from './MyComponent';
import React from 'react';
import lodash from 'lodash';
import { Text } from 'react-native';
import { View } from 'react-native'; import MyComponent from './MyComponent'; import React from 'react'; import lodash from 'lodash'; import { Text } from 'react-native';
import { View } from 'react-native';
import MyComponent from './MyComponent';
import React from 'react';
import lodash from 'lodash';
import { Text } from 'react-native';

After organizing and sorting:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import React from 'react';
import { View, Text } from 'react-native';
import lodash from 'lodash';
import MyComponent from './MyComponent';
import React from 'react'; import { View, Text } from 'react-native'; import lodash from 'lodash'; import MyComponent from './MyComponent';
import React from 'react';
import { View, Text } from 'react-native';
import lodash from 'lodash';

import MyComponent from './MyComponent';

In this example, imports are organized into two groups: external libraries (like ‘react’ and ‘lodash’) and local imports (like ‘./MyComponent’). Within each group, imports are sorted alphabetically.

10.Code Refactoring:

Code refactoring is the process of restructuring existing code without changing its external behavior. Its primary purpose is to improve nonfunctional attributes of the software.

Example:

Consider a React Native component that mixes UI rendering with data fetching logic.

Before refactoring:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const UserProfile = ({ userId }) => {
const [user, setUser] = useState(null);
useEffect(() => {
const fetchUser = async () => {
const response = await fetch(`https://api.example.com/users/${userId}`);
const userData = await response.json();
setUser(userData);
};
fetchUser();
}, [userId]);
return user ? <Text>Welcome, {user.name}!</Text> : <Text>Loading...</Text>;
};
const UserProfile = ({ userId }) => { const [user, setUser] = useState(null); useEffect(() => { const fetchUser = async () => { const response = await fetch(`https://api.example.com/users/${userId}`); const userData = await response.json(); setUser(userData); }; fetchUser(); }, [userId]); return user ? <Text>Welcome, {user.name}!</Text> : <Text>Loading...</Text>; };
const UserProfile = ({ userId }) => {
  const [user, setUser] = useState(null);

  useEffect(() => {
    const fetchUser = async () => {
      const response = await fetch(`https://api.example.com/users/${userId}`);
      const userData = await response.json();
      setUser(userData);
    };

    fetchUser();
  }, [userId]);

  return user ? <Text>Welcome, {user.name}!</Text> : <Text>Loading...</Text>;
};

After refactoring:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const useFetchUser = (userId) => {
const [user, setUser] = useState(null);
useEffect(() => {
const fetchUser = async () => {
const response = await fetch(`https://api.example.com/users/${userId}`);
const userData = await response.json();
setUser(userData);
};
fetchUser();
}, [userId]);
return user;
};
const UserProfile = ({ userId }) => {
const user = useFetchUser(userId);
return user ? <Text>Welcome, {user.name}!</Text> : <Text>Loading...</Text>;
};
const useFetchUser = (userId) => { const [user, setUser] = useState(null); useEffect(() => { const fetchUser = async () => { const response = await fetch(`https://api.example.com/users/${userId}`); const userData = await response.json(); setUser(userData); }; fetchUser(); }, [userId]); return user; }; const UserProfile = ({ userId }) => { const user = useFetchUser(userId); return user ? <Text>Welcome, {user.name}!</Text> : <Text>Loading...</Text>; };
const useFetchUser = (userId) => {
  const [user, setUser] = useState(null);

  useEffect(() => {
    const fetchUser = async () => {
      const response = await fetch(`https://api.example.com/users/${userId}`);
      const userData = await response.json();
      setUser(userData);
    };

    fetchUser();
  }, [userId]);

  return user;
};

const UserProfile = ({ userId }) => {
  const user = useFetchUser(userId);

  return user ? <Text>Welcome, {user.name}!</Text> : <Text>Loading...</Text>;
};

In the refactored example, the data fetching logic is extracted into a custom hook (useFetchUser). This makes the UserProfile component more focused on rendering the UI and easier to maintain. The custom hook can also be reused across other components if needed.

These practices are part of writing clean and maintainable code in React Native, allowing for better collaboration and easier updates to the codebase over time.

Additional Resources

Leave a Comment