"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to start with React Native as ReactJS developer?

How to start with React Native as ReactJS developer?

Published on 2024-08-07
Browse:673

How to start with React Native as ReactJS developer?

Recently, I spoke at React Nexus on "Accessibility and TV Apps." One question I kept getting was: "As a ReactJS developer, how easy is it to start with React Native?"

In short, for a ReactJS developer, starting with React Native would be easy.

In this blog, I am going to share the five things ReactJS developers can use in React Native.

1. Components

In React Native, you will create components similarly to how you do in ReactJS. The concepts and best practices remain the same.

import React from 'react';
import { View, Text } from 'react-native';

const GreetingComponent = () => {
  return (
    Hello, Neha!
  );
};
export default GreetingComponent;

2. Props and state

In React Native, props and state work the same way as in ReactJS. To communicate between components, you will use props. To update values, you will use state.

import React from 'react';
import { View, Text } from 'react-native';

const GreetingComponent = ({ name }) => {
  return (
    Hello, {name}!
  );
};
export default GreetingComponent;

3. Hooks

Just like in ReactJS, you can use all the hooks in React Native, such as useState(), useMemo(), useEffect(), etc. Additionally, you can create your own custom hooks.

import React, { useState } from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';

const GreetingComponent = () => {
  const [name, setName] = useState('John');

  const changeName = () => {
    setName('Jane');
  };

  return (
    Hello, {name}!
    
  );
};

export default GreetingComponent;

4. Testing

If you are a fan of the React Testing Library, the good news is you can use the same library for testing in React Native.

import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import GreetingComponent from './GreetingComponent';

test('it renders correctly and changes name on button press', () => {
  // Render the component
  const { getByText } = render();

  // Assert initial state
  expect(getByText('Hello, John!')).toBeTruthy();

  // Find the button and simulate a press
  const button = getByText('Change Name');
  fireEvent.press(button);

  // Assert that the name has changed
  expect(getByText('Hello, Jane!')).toBeTruthy();
});

5. JSX

In React Native, there are a handful of components that can be used to create views in JSX. However, in ReactJS, you can use any valid HTML DOM elements.

import React from 'react';
import { View, Text } from 'react-native';

const GreetingComponent = () => {
  return (
    Hello, Neha!
  );
};
export default GreetingComponent;

Happy Learning!!

Release Statement This article is reproduced at: https://dev.to/hellonehha/how-to-start-with-react-native-as-reactjs-developer-2d61?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3