Categories
React Native Answers

How to hide the navigation navbar with React Native?

To hide the navigation navbar with React Native, we set the screenOptions.headerShown option to false.

For instance, we write

<Stack.Navigator
  screenOptions={{
    headerShown: false,
  }}
>
  ...
</Stack.Navigator>;

to set the headerShown option of the screenOptions prop object to false on the Stack.Navigator to hide the navbar.

Categories
React Native Answers

How to fix the React Native navigation paramlist never used error?

To fix the React Native navigation paramlist never used error, we add '@typescript-eslint/parser' into .eslintrc.

For instance, in .eslintrc, we write

{
  "parser": "@typescript-eslint/parser"
}

We also need to download ‘@typescript-eslint/parser’ in your dev dependencies

To do this, we run

npm i @typescript-eslint/parser --save-dev
Categories
React Native Answers

How to populate the right side of the navigation header with React Native?

To populate the right side of the navigation header with React Native, we set the screnOptions prop o

<Stack.Navigator>
  <Stack.Group
    screenOptions={({ route, navigation }) => ({
      headerRight: () => <Button onPress={() => navigation.navigate("Home")} />,
    })}
  >
    <Stack.Screen name="Home" component={HomeScreen} />
    <Stack.Screen name="Profile" component={ProfileScreen} />
  </Stack.Group>
  <Stack.Group screenOptions={{ presentation: "modal" }}>
    <Stack.Screen name="Settings" component={Settings} />
    <Stack.Screen name="Share" component={Share} />
  </Stack.Group>
</Stack.Navigator>;

to add the Stack.Group‘s screenOptions prop.

We set it to a function that has a button on the right by setting headerRight to a function that renders a Button.

Categories
React Answers

How to write unit test cases in React?

To write unit tests in React, we can use the react-dom/test-utils package.

For instance, we write

import React from "react";

export default function Hello(props) {
  if (props.name) {
    return <h1>Hello, {props.name}!</h1>;
  } else {
    return <span>Hey, stranger</span>;
  }
}

in hello.js.

Then in hello.test.js, we write

import React from "react";
import { render, unmountComponentAtNode } from "react-dom";
import { act } from "react-dom/test-utils";

import Hello from "./hello";

let container = null;
beforeEach(() => {
  container = document.createElement("div");
  document.body.appendChild(container);
});

afterEach(() => {
  // cleanup on exiting
  unmountComponentAtNode(container);
  container.remove();
  container = null;
});

it("renders with or without a name", () => {
  act(() => {
    render(<Hello />, container);
  });
  expect(container.textContent).toBe("Hey, stranger");

  act(() => {
    render(<Hello name="Jenny" />, container);
  });
  expect(container.textContent).toBe("Hello, Jenny!");

  act(() => {
    render(<Hello name="Margaret" />, container);
  });
  expect(container.textContent).toBe("Hello, Margaret!");
});

to add a test for the Hello component.

We mount the component with render

Then we check the rendered content with expect.

The beforeEach callback runs before each test.

We use

container = document.createElement("div");

to create the render target element.

And the afterEach callback runs after each test.

We clean up after our tests in the afterEach callback.

Categories
React Native Answers

How to remove top header from the navigation with React Native?

To remove top header from the navigation with React Native, we can set the static navigationOptions.header propety to null or callnavigation.setOptions.

For instance, we write

export default class Login extends Component {
  static navigationOptions = {
    header: null,
  };
}

to set the navigationOptions.header property to null in our component to remove the header.

Also, we can write

setTimeout(() => {
  navigation.setOptions({
    header: () => (
      <View style={{ backgroundColor: "white" }}>
        <Text
          style={[
            { color: "white" },
            Platform.OS === "android" ? { fontSize: 20 } : { fontSize: 1 },
          ]}
        >
          .
        </Text>
      </View>
    ),
  });
}, 1);

to call call navigation.setOptions with an object with the header property set to a white View to hide the header.