Categories
React Answers

How to Prevent Scrolling Using CSS on React Rendered Components?

Sometimes, we want to prevent scrolling using CSS on React rendered components.

In this article, we’ll look at how to prevent scrolling using CSS on React rendered components.

Prevent Scrolling Using CSS on React Rendered Components

To prevent scrolling using CSS on React rendered components, we can set the overflow CSS property to hidden with JavaScript.

For instance, we write:

import React, { useEffect } from "react";

export default function App() {
  useEffect(() => {
    document.body.style.overflow = "hidden";
  }, []);

  return <div>hello world</div>;
}

to set the overflow CSS of the body element to hidden when the component mounts with:

document.body.style.overflow = "hidden";

The useEffect callback only runs when the component mounts since we passed in an empty array as the 2nd argument of useEffect.

Conclusion

To prevent scrolling using CSS on React rendered components, we can set the overflow CSS property to hidden with JavaScript.

Categories
React Answers

How to Concatenate JSX Elements into an Array?

Sometimes, we want to concatenate JSX elements into an array.

In this article, we’ll look at how to concatenate JSX elements into an array.

Concatenate JSX Elements into an Array

To concatenate JSX elements into an array, we can call the JavaScript array’s push instance method.

For instance, we write:

import React from "react";

export default function App() {
  const buffer = [];
  buffer.push(<div>A</div>);
  buffer.push(<div>B</div>);
  buffer.push(<div>C</div>);

  return <div>{buffer}</div>;
}

to define the buffer array.

Then we call push with different elements we want to put into it.

And then we render the buffer array’s contents on the screen by putting it in between the braces.

Now we should see:

A
B
C

displayed.

Conclusion

To concatenate JSX elements into an array, we can call the JavaScript array’s push instance method.

Categories
React Answers

How to Open a Component in New Window on a Click in React?

Sometimes, we want to open a component in new window on a click in React.

In this article, we’ll look at how to open a component in new window on a click in React.

Open a Component in New Window on a Click in React

To open a component in new window on a click in React, we can call window.open with the element we created.

For instance, we write:

import React, { useEffect, useRef, useState } from "react";
import { createPortal } from "react-dom";

const RenderInWindow = (props) => {
  const [container, setContainer] = useState(null);
  const newWindow = useRef(window);

  useEffect(() => {
    const div = document.createElement("div");
    setContainer(div);
  }, []);

  useEffect(() => {
    if (container) {
      newWindow.current = window.open(
        "",
        "",
        "width=600,height=400,left=200,top=200"
      );
      newWindow.current.document.body.appendChild(container);
      const curWindow = newWindow.current;
      return () => curWindow.close();
    }
  }, [container]);

  return container && createPortal(props.children, container);
};

export default function App() {
  const [open, setOpen] = useState();
  return (
    <>
      <button onClick={() => setOpen(true)}>open</button>
      {open && <RenderInWindow>hello world</RenderInWindow>}
    </>
  );
}

to create the RenderInWindow component that opens a new window when it mounts.

To do this, we create the container state with useState.

And we create a ref to store the window object.

In the first useEffect callback, we create a div that we use as the content of the element when RenderInWindow mounts and call setContainer to set the div as the value of `container.

In the 2nd useEffect callback, we check if container is set.

If it is, then we call window.open and set the returned window object as the value of newWindow.current.

And we set the content of the window with:

newWindow.current.document.body.appendChild(container);

And then we get newWindow.current and set it to curWindow.

Finally we return a function that calls curWindow.close to close the window when the component unmounts.

Then we return a portal with the window.

We get the content of the window from the children prop.

In App, we render the RenderInWindow component with the content of the popup window when open is true.

And we make open true when we click on open.

Therefore, we should see ‘hello world’ displayed in a popup window when we click on open.

Conclusion

To open a component in new window on a click in React, we can call window.open with the element we created.

Categories
React Answers

How to Fix the “‘await’ has no effect on the type of this expression” warning in React?

Sometimes, we may run into the "’await’ has no effect on the type of this expression" warning in React.

In this article, we’ll look at how to fix the "’await’ has no effect on the type of this expression" warning in React.

Fix the "’await’ has no effect on the type of this expression" warning in React

To fix the "’await’ has no effect on the type of this expression" warning in React, we should make sure that we only use await on expressions that returns a promise.

For instance, we write:

import React, { useEffect, useState } from "react";

const fetchAnswer = async () => {
  const response = await fetch(`https://yesno.wtf/api`);
  const json = await response.json();
  return json;
};

export default function App() {
  const [ans, setAns] = useState();

  useEffect(() => {
    const getAnswer = async () => {
      const { answer } = await fetchAnswer();
      setAns(answer);
    };
    getAnswer();
  }, []);

  return (
    <>
      <div>{ans}</div>
    </>
  );
}

to clear the warning.

We have fetchAnswer function that makes a GET request with fetch.

Then we return the JSON response from the API by calling response.json and get the resolved value with await.

Then we return the json.

Next, we use the JSON response data in the getAnswer function.

It calls fetchAnswer to return a promise with the JSON response data.

And then we call serAns with the resolved value that we get with await.

Finally, we display the ans value on the screen by returning it.

Conclusion

To fix the "’await’ has no effect on the type of this expression" warning in React, we should make sure that we only use await on expressions that returns a promise.

Categories
React Answers

How to Set Text Color in React?

Sometimes, we want to set text color in React.

In this article, we’ll look at how to set text color in React.

Set Text Color in React

To set text color in React, we can set the style prop to an object with the color property.

For instance, we write:

import React from "react";

export default function App() {
  return (
    <>
      <h1 style={{ color: "red" }}>Hello world</h1>
    </>
  );
}

to set the style prop of the h1 element to an object that has the color property set to 'red'.

Now we should see that the color of the text is red.

Conclusion

To set text color in React, we can set the style prop to an object with the color property.