Categories
React Answers

How to fix Objects are not valid as a React child (found: [object Promise]) error?

Sometimes, we want to fix Objects are not valid as a React child (found: [object Promise]) error.

in this article, we’ll look at how to fix Objects are not valid as a React child (found: [object Promise]) error.

How to fix Objects are not valid as a React child (found: [object Promise]) error?

To fix Objects are not valid as a React child (found: [object Promise]) error, we sure make sure we aren’t creating function components with async functions.

For instance, we write

const HelloApp = (props) => {
  return (
    <div>
      <h2>Hello World</h2>
    </div>
  );
};
ReactDOM.render(<HelloApp />, document.querySelector("#app"));

to create the HelloApp component with a regular JavaScript function.

Then this error should go away since regular functions can return JSX.

Conclusion

To fix Objects are not valid as a React child (found: [object Promise]) error, we sure make sure we aren’t creating function components with async functions.

Categories
React Answers

How to intercept or handle browser’s back button in React Router v5?

Sometimes, we want to intercept or handle browser’s back button in React Router v5.

In this article, we’ll look at how to intercept or handle browser’s back button in React Router v5.

How to intercept or handle browser’s back button in React Router v5?

To intercept or handle browser’s back button in React Router v5, we can use the Prompt component.

For instance, we write

<Prompt
  message={(location, action) => {
    if (action === "POP") {
      // ...
    }
    return true;
  }}
/>;

to add the Prompt component with the message prop set to a function that runs when we navigate.

In it, we check if action is 'POP'.

If it is, then we’re navigating with the back button.

Conclusion

To intercept or handle browser’s back button in React Router v5, we can use the Prompt component.

Categories
React Answers

How to fix ‘Expected corresponding JSX closing tag for input’ error with React?

Sometimes, we want to fix ‘Expected corresponding JSX closing tag for input’ error with React.

In this article, we’ll look at how to fix ‘Expected corresponding JSX closing tag for input’ error with React.

How to fix ‘Expected corresponding JSX closing tag for input’ error with React?

To fix ‘Expected corresponding JSX closing tag for input’ error with React, we should close the tags in our element.

For instance, we write

<input type="text" class="validate" />

to add a slash to close the input tag in our JSX code.

Then the error will go away.

Conclusion

To fix ‘Expected corresponding JSX closing tag for input’ error with React, we should close the tags in our element.

Categories
React Answers

How to render an array of objects in React?

Sometimes, we want to render an array of objects in React.

In this article, we’ll look at how to render an array of objects in React.

How to render an array of objects in React?

To render an array of objects in React, we can use the array map method.

For instance, we write

const Item = (props) => {
  return <li>{props.message}</li>;
};

const TodoList = () => {
  const todos = ["foo", "bar", "baz"];

  return (
    <ul>
      {todos.map((message) => (
        <Item key={message} message={message} />
      ))}
    </ul>
  );
};

to call todos.map with a callback toi render the Item component with the message value.

We set the key prop to a unique value for each entry so that React can identify each item being rendered.

Conclusion

To render an array of objects in React, we can use the array map method.

Categories
React Answers

How to put an icon inside a TextInput in React Native?

Sometimes, we want to put an icon inside a TextInput in React Native.

In this article, we’ll look at how to put an icon inside a TextInput in React Native.

How to put an icon inside a TextInput in React Native?

To put an icon inside a TextInput in React Native, we can use the TextInput component in the react-native-paper module.

We import it with

import { TextInput } from "react-native-paper";

Then we add the TextInput with the icon with

<TextInput
  label="Password"
  secureTextEntry
  right={<TextInput.Icon name="eye" />}
/>;

We set the right prop to the Icon to show the eye icon on the right of the input.

Conclusion

To put an icon inside a TextInput in React Native, we can use the TextInput component in the react-native-paper module.