Categories
React Native Answers

How to add another VirtualizedList-backed container with React-Native?

Sometimes, we want to add another VirtualizedList-backed container with React-Native.

In this article, we’ll look at how to add another VirtualizedList-backed container with React-Native.

How to add another VirtualizedList-backed container with React-Native?

To add another VirtualizedList-backed container with React-Native, we can set the ListHeaderComponent and ListFooterComponent props of the FlatList.

For instance, we write

<SafeAreaView style={{ flex: 1 }}>
  <FlatList
    data={data}
    ListHeaderComponent={ContentThatGoesAboveTheFlatList}
    ListFooterComponent={ContentThatGoesBelowTheFlatList}
  />
</SafeAreaView>

to set ListHeaderComponent prop to the ContentThatGoesAboveTheFlatList component and ListFooterComponent prop to the ContentThatGoesBelowTheFlatList component.

Then all the components will show beside the FlatList.

We put them in the SafeAreaView component to render the FlatList within the safe area boundaries of the device.

Conclusion

To add another VirtualizedList-backed container with React-Native, we can set the ListHeaderComponent and ListFooterComponent props of the FlatList.

Categories
JavaScript Answers

How to iterate over object keys in Node.js and JavaScript?

Sometimes, we want to iterate over object keys in Node.js and JavaScript

In this article, we’ll look at how to iterate over object keys in Node.js and JavaScript.

How to iterate over object keys in Node.js and JavaScript?

To iterate over object keys in Node.js and JavaScript, we use the Object.keys method to get the object’s non-inherited keys in an array.

For instance, we write

Object.keys(obj).forEach((key) => {
  //...
});

to call Object.keys with obj to return an array of non-inherited object string keys.

Then we call forEach with a callback that gets the key from the callback parameter.

Conclusion

To iterate over object keys in Node.js and JavaScript, we use the Object.keys method to get the object’s non-inherited keys in an array.

Categories
JavaScript Answers

How to get the arguments called in a Jest mock function with JavaScript?

Sometimes, we want to get the arguments called in a Jest mock function with JavaScript.

In this article, we’ll look at how to get the arguments called in a Jest mock function with JavaScript.

How to get the arguments called in a Jest mock function with JavaScript?

To get the arguments called in a Jest mock function with JavaScript, we call toHaveBeenCalledWith.

For instance, we write

expect(mockedFunction).toHaveBeenCalledWith("param1", "param2");

to call expect with the function we mocked.

And then we call toHaveBeenCalledwith with the arguments that we want to check the mockedFunction is called with.

This is done in our test callback.

Conclusion

To get the arguments called in a Jest mock function with JavaScript, we call toHaveBeenCalledWith.

Categories
React Answers

How to do a redirect to another route with React Router v6?

Sometimes, we want to do a redirect to another route with React Router v6.

In this article, we’ll look at how to do a redirect to another route with React Router v6.

How to do a redirect to another route with React Router v6?

To do a redirect to another route with React Router v6, we can use the useNavigate hook.

For instance, we write

import React from "react";
import { useNavigate } from "react-router-dom";

const Comp = () => {
  const navigate = useNavigate();

  const handleClick = () => {
    navigate("/path/to/push");
  };

  return (
    <div>
      <button onClick={handleClick} type="button" />
    </div>
  );
};

export default Comp;

to call the useNavigate hook to return the navigate function.

Then we create the handleClick function that calls navigate with the path we want to go to.

Then we set onClick to handleClick to go to the path when we click on the button.

Conclusion

To do a redirect to another route with React Router v6, we can use the useNavigate hook.

Categories
JavaScript Answers

How to call finally on subscribing to observable with Rxjs and JavaScript?

Sometimes, we want to to call finally on subscribing to observable with Rxjs and JavaScript.

In this article, we’ll look at how to to call finally on subscribing to observable with Rxjs and JavaScript.

How to call finally on subscribing to observable with Rxjs and JavaScript?

To to call finally on subscribing to observable with Rxjs and JavaScript, we call pipe on the observable we subscribed to.

For instance, we write

const source = new Observable((observer) => {
  observer.next(1);
  observer.error("error message");
  observer.next(3);
  observer.complete();
}).pipe(publish());

source.pipe(finalize(() => console.log("Finally callback"))).subscribe(
  (value) => console.log(value),
  (error) => console.log(error),
  () => console.log("complete")
);

to create an Observable.

And then we call pipe to emit the data.

Then we call pipe with the observable returned by finalize.

We call finalize with a callback that runs when all the observable code is run.

pipe returns another observable that we call subscribe on to get the data from pipe.

Conclusion

To to call finally on subscribing to observable with Rxjs and JavaScript, we call pipe on the observable we subscribed to.