Today I will try to share how to solving and solution error "validateDOMNesting body cannot appear as a child of ", There are so many things that cause this problem, but for my situation, I can solve this problem like example scripts below :
The Redirect component was usually used in previous versions of the react-router-dom package to quickly do redirects by simply importing the component from react-router-dom and then making use of the component by providing the to prop, passing the page you desire to redirect to.
With the release of React Router v6, the Redirect component was removed and replaced with the Navigate component, which operates just as the Redirect component does by taking in the to prop to enable you to redirect to the page you specify.
Following example scripts of solution with Navigate scripts :
Today I will try to share information or tutorial about "How to show script from variable or function with html scripts value result into JSX React ". This tutorial may be rare and short, but hope this information can help you. Okay lets see piece of code below :
This is a source code function that return html value
If you work with react and sometimes you want your web to do not refreshing the page when you click button submit. The solution of this problem are just try this scripts below (for example) :
consthandleSubmit=event=>{console.log('handleSubmit ran');
event.preventDefault();// 👈️ prevent page refresh// 👇️ access input values hereconsole.log('firstName 👉️', firstName);console.log('lastName 👉️', lastName);// 👇️ clear all input values in the formsetFirstName('');setLastName('');};
That is solution " submit form without refreshing page in react". Hope this example can helping you.
Among the changes in React 16.3 is a new stable version of the Context API. We’re going to take a look at how it works by building a protected route component.
What is Context?
Context is about encapsulating state. It allows us to pass data from a parent provider component to any subscribed component down the tree. Without state management we often have to “drill” props through every component along the way.
Isn’t that what Redux is for?
Yes, Context operates similarly to how components can connect to Redux’s global state. However, a native element like Context will often be a better solution for small to medium apps that don’t need the complex overhead of Redux.
Basic Concepts
There are three elements to Context:
createContext — Calling this returns a pair of components, Provider and Consumer.
Provider — a Component that allows for one or more Consumers to subscribe to changes.
Consumer —a Component subscribed to a Provider
Let’s Start Building
We’re going to build an app with two routes. One is a landing page with global access. The other is a dashboard page with restricted access for logged in users. You can find the final version here.
Try it out: go to /dashboard while logged out. Log in and navigate freely between routes. From the dashboard, log out and it’ll kick you out to the landing page.
Context Header
To demonstrate Context’s basic functionality, let’s start by building a header component that lets us log in and out. First, create our context in a new file.
/* AuthContext.js */import React from 'react';const AuthContext = React.createContext();
Export a component AuthProvider to define our state (whether the user is logged in) and pass its state to the value prop on the Provider. We’ll simply expose AuthConsumer with a meaningful name.
Now that we have covered the basics, let’s extend what we’ve learned to create a protected route component.
First make Landing and Dashboard page components. Our dashboard will only be visible when the user is logged in. Both pages will be as simple, as below:
/* index.js */ import React from 'react'; import { render } from 'react-dom'; import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; import { AuthProvider } from './AuthContext'; import Landing from './Landing'; import Dashboard from './Dashboard'; import Header from './Header';const App = () => ( <div> <Router> <AuthProvider> <Header /> <Switch> <Route path="/dashboard" component={Dashboard} /> <Route path="/" component={Landing} /> </Switch> </AuthProvider> </Router> </div> );render(<App />, document.getElementById('root'));
In this current state you can navigate to both / and /dashboard. We’ll make a special route component that checks if a user is logged in called ProtectedRoute. The set up is similar to our Header component.