Categories
React Answers

How to fix Objects are not valid as a React child. If you meant to render a collection of children, use an array instead with JavaScript?

Sometimes, we want o fix Objects are not valid as a React child. If you meant to render a collection of children, use an array instead with JavaScript.

In this article, we’ll look at how to fix Objects are not valid as a React child. If you meant to render a collection of children, use an array instead with JavaScript.

How to fix Objects are not valid as a React child. If you meant to render a collection of children, use an array instead with JavaScript?

To fix Objects are not valid as a React child. If you meant to render a collection of children, use an array instead with JavaScript, we should make sure we only render valid JSX in our React component.

For instance, we write

export default () => {
  //..
  return (
    <div className="col">
      <h1>Mi Casa</h1>
      <p>This is my house y&apos;all!</p>
      {homes.map((home) => (
        <div>{home.name}</div>
      ))}
    </div>
  );
};

to render elements an an array that returns an array of elements in our component.

We call map with a callback to render the homes array into an array of elements that can be rendered with React.

They’re all valid JSX code so no error will be thrown.

Conclusion

To fix Objects are not valid as a React child. If you meant to render a collection of children, use an array instead with JavaScript, we should make sure we only render valid JSX in our React component.

Categories
React Answers

How to fix Invalid hook call. Hooks can only be called inside of the body of a function component with React and JavaScript?

Sometimes, we want to fix Invalid hook call. Hooks can only be called inside of the body of a function component with React and JavaScript.

in this article, we’ll look at how to fix Invalid hook call. Hooks can only be called inside of the body of a function component with React and JavaScript.

How to fix Invalid hook call. Hooks can only be called inside of the body of a function component with React and JavaScript?

To fix Invalid hook call. Hooks can only be called inside of the body of a function component with React and JavaScript, we should make sure we call hook functions only on the top level of function components.

For instance, we write

export default () => {
  const classes = useStyles();
  return <AllowanceClass classes={classes} />;
};

to call the useStyles hook at the top of the component.

And then we can use its returned value anywhere in the component.

Conclusion

To fix Invalid hook call. Hooks can only be called inside of the body of a function component with React and JavaScript, we should make sure we call hook functions only on the top level of function components.

Categories
React Answers

How to set the correct path for img on React.js and JavaScript?

Sometimes, we want to set the correct path for img on React.js and JavaScript.

In this article, we’ll look at how to set the correct path for img on React.js and JavaScript.

How to set the correct path for img on React.js and JavaScript?

To set the correct path for img on React.js and JavaScript, we can import the image to get the right path.

For instance, we write

import logo from "./logo.png";

class Nav extends Component {
  render() {
    return <img src={logo} alt={"logo"} />;
  }
}

to import the logo.png with import.

And then we set the img element’s src attribute to logo to load the image.

This works with Create React app projects since it has the image loader built in that lets us import images with import.

Conclusion

To set the correct path for img on React.js and JavaScript, we can import the image to get the right path.

Categories
React Answers

How to use Redirect in React react-router-dom v5?

Sometimes, we want to use Redirect in React react-router-dom v5.

In this article, we’ll look at how to use Redirect in React react-router-dom v5.

How to use Redirect in React react-router-dom v5?

To use Redirect in React react-router-dom v5, we call history.push.

For instance, we write

import { useHistory } from "react-router-dom";

function HomeButton() {
  const history = useHistory();

  const handleClick = () => {
    history.push("/home");
  };

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

to call the useHistory hook to return the history object.

Then we call history.push to go to the '/home' URL.

And then we set onClick prop to handleClick to do the redirect when we click the button.

Conclusion

To use Redirect in React react-router-dom v5, we call history.push.

Categories
React Answers

How to listen for state changes in React?

Sometimes, we want to listen for state changes in React.

In this article, we’ll look at how to listen for state changes in React.

How to listen for state changes in React?

To listen for state changes in React, we use the useEffect hook.

For instance, we write

export function MyComponent(props) {
  const [myState, setMystate] = useState("initialState");

  useEffect(() => {
    console.log(myState);
  }, [myState]);
  //...
}

to create the MyComponent component.

In it, we define the myState state.

And we watch it for changes with

useEffect(() => {
  console.log(myState);
}, [myState]);

We call the useEffect hook with a callback that logs the latest value of myState.

And we use [myState] as the 2nd argument to watch myState for changes.

Conclusion

To listen for state changes in React, we use the useEffect hook.