Reactjs 如何修复反应警告
我在浏览器中收到此警告 警告:validateDOMNesting(…):不能显示为的后代 . 在a中(在App.js:79) 在中(由LinkAnchor创建) in-LinkAnchor(由Context.Consumer创建) 链接中(在App.js:79) 导航中(见App.js:77) 页眉中(在App.js:70处) 在div中(在App.js:68中) 路由器中(由BrowserRouter创建) 浏览器路由器中(位于App.js:67) 应用内(由ConnectFunction创建) 在ConnectFunction中(位于src/index.js:23) 在提供程序中(位于src/index.js:22) 从下面的代码Reactjs 如何修复反应警告,reactjs,react-router-dom,Reactjs,React Router Dom,我在浏览器中收到此警告 警告:validateDOMNesting(…):不能显示为的后代 . 在a中(在App.js:79) 在中(由LinkAnchor创建) in-LinkAnchor(由Context.Consumer创建) 链接中(在App.js:79) 导航中(见App.js:77) 页眉中(在App.js:70处) 在div中(在App.js:68中) 路由器中(由BrowserRouter创建) 浏览器路由器中(位于App.js:67) 应用内(由ConnectFunction创
<Link to={'/addRobo'} className="nav-link"><a className="f6 f5-l link bg-animate black-80 hover-bg-light-green dib pa3 ph4-l" href="1">Add Robot</a></Link>
还有这个代码
import React, {
Component
} from 'react';
import {
Link,
Route,
Switch
} from 'react-router-dom';
import {
BrowserRouter as Router
} from 'react-router-dom';
import {
connect
} from 'react-redux';
import {
setSearchField,
requestRobots
} from '../actions';
import CardList from '../components/CardList';
import SearchBox from '../components/SearchBox';
import Scroll from '../components/Scroll';
import ErrorBoundry from '../components/ErrorBoundry';
import AddRobot from '../components/AddRobot';
import UppdateRobot from '../components/UpdateRobot';
import DeleteRobot from '../components/DeleteRobot';
import './App.css';
// parameter state comes from index.js provider store state(rootReducers)
const mapStateToProps = (state) => {
return {
searchField: state.searchRobots.searchField,
robots: state.manage_data.robots,
isPending: state.manage_data.isPending
}
}
// dispatch the DOM changes to call an action. note mapStateToProps returns object, mapDispatchToProps returns function
// the function returns an object then uses connect to change the data from redecers.
const mapDispatchToProps = (dispatch) => {
return {
onSearchChange: (event) => dispatch(setSearchField(event.target.value)),
onRequestRobots: () => dispatch(requestRobots()),
}
}
class App extends Component {
componentDidMount() {
console.log(this.props.userinfo);
this.props.onRequestRobots();
}
render() {
const {
robots,
searchField,
onSearchChange,
isPending
} = this.props;
const stylecur = {
fill: 'currentcolor'
};
const filteredRobots = robots.filter(robot => {
return robot.name.toLowerCase().includes(searchField.toLowerCase());
})
const routes = [{
id: 1,
path: '/addRobo',
component: AddRobot
},
{
id: 2,
path: '/updateRobo',
component: UppdateRobot
},
{
id: 3,
path: '/searchRobo',
component: SearchBox
},
{
id: 4,
path: '/delRobo',
component: DeleteRobot
}
];
return ( <
Router >
<
div className = 'tc' >
<
header className = "bg-white black-80 tc pv4 avenir" >
<
a href = ""
className = "bg-black-80 ba b--black dib pa3 w2 h2 br-100" >
<
svg className = "white"
data - icon = "skull"
viewBox = "0 0 32 32"
style = {
stylecur
} >
<
title > skull icon < /title><path d="M16 0 C6 0 2 4 2 14 L2 22 L6 24 L6 30 L26 30 L26 24 L30 22 L30 14 C30 4 26 0 16 0 M9 12 A4.5 4.5 0 0 1 9 21 A4.5 4.5 0 0 1 9 12 M23 12 A4.5 4.5 0 0 1 23 21 A4.5 4.5 0 0 1 23 12"></path > < /svg> <
/a> <
h1 className = "mt2 mb0 baskerville i fw1 f1" > RoboFriends < /h1> <
h2 className = "mt2 mb0 f6 fw4 ttu tracked" > Create an ensembe of Robot with your friends! < /h2> <
nav className = "bt bb tc mw7 center mt4" >
<
a className = "f6 f5-l link bg-animate black-80 hover-bg-lightest-blue dib pa3 ph4-l"
href = "/" > Home < /a> <
Link to = {
'/addRobo'
}
className = "nav-link" > < a className = "f6 f5-l link bg-animate black-80 hover-bg-light-green dib pa3 ph4-l"
href = "1" > Add Robot < /a></Link >
<
Link to = {
'/updateRobo'
}
className = "nav-link" > < a className = "f6 f5-l link bg-animate black-80 hover-bg-light-blue dib pa3 ph4-l" > Update Robot < /a></Link >
<
Link to = {
'/searchRobo'
}
className = "nav-link" > < a className = "f6 f5-l link bg-animate black-80 hover-bg-light-pink dib pa3 ph4-l" > Search Robot < /a></Link >
<
a className = "f6 f5-l link bg-animate black-80 hover-bg-light-yellow dib pa3 ph4-l" > < Link to = {
'/delRobo'
}
className = "nav-link" > Delete Robot < /Link></a >
<
/nav> <
/header> <
Switch > {
routes.map(({
id,
path,
component: Component
}) => ( <
Route key = {
id
}
path = {
path
}
component = {
Component
} // sans the angle brackets </>
/>))
} <
/Switch> <
Scroll > {
isPending ? < h1 > Loading < /h1> : <
ErrorBoundry >
<
CardList robots = {
filteredRobots
}
/> <
/ErrorBoundry>
} <
/Scroll> <
/div> <
/Router>
);
}
}
// action done from mapDispatchToProps will channge state from mapStateToProps
export default connect(mapStateToProps, mapDispatchToProps)(App)
import-React{
组成部分
}从"反应",;
进口{
链接
路线,,
转换
}从“反应路由器dom”;
进口{
浏览器路由器作为路由器
}从“反应路由器dom”;
进口{
连接
}来自“react redux”;
进口{
塞斯菲尔德,
请求机器人
}来自“../actions”;
从“../components/CardList”导入卡片列表;
从“../components/SearchBox”导入SearchBox;
从“../components/Scroll”导入滚动;
从“../components/ErrorBoundry”导入ErrorBoundry;
从“../components/AddRobot”导入AddRobot;
从“../components/UpdateRobot”导入UppdateRobot;
从“../components/DeleteRobot”导入DeleteRobot;
导入“/App.css”;
//参数状态来自index.js提供程序存储状态(rootReducers)
常量mapStateToProps=(状态)=>{
返回{
searchField:state.searchRobots.searchField,
机器人:state.manage_data.robots,
isPending:state.manage\u data.isPending
}
}
//分派DOM更改以调用操作。注意:mapStateToProps返回对象,mapDispatchToProps返回函数
//该函数返回一个对象,然后使用connect更改redecers中的数据。
const mapDispatchToProps=(调度)=>{
返回{
onSearchChange:(事件)=>调度(setSearchField(event.target.value)),
onRequestRobots:()=>调度(requestRobots()),
}
}
类应用程序扩展组件{
componentDidMount(){
log(this.props.userinfo);
this.props.onRequestRobots();
}
render(){
常数{
机器人,
searchField,
在研究变化方面,
散播
}=这是道具;
常量stylecur={
填充:“当前颜色”
};
常量过滤器机器人=机器人。过滤器(机器人=>{
return robot.name.toLowerCase().includes(searchField.toLowerCase());
})
常数路由=[{
id:1,
路径:'/addRobo',
组件:AddRobot
},
{
id:2,
路径:'/updateRobo',
组件:机器人
},
{
id:3,
路径:'/searchRobo',
组件:搜索框
},
{
id:4,
路径:'/delRobo',
组件:DeleteRobot
}
];
报税表(<
路由器>
<
div className='tc'>
<
header className=“bg黑白-80 tc pv4 avenir”>
<
a href=“”
className=“bg-black-80 ba b——黑色dib pa3 w2 h2 br-100”>
<
svg className=“白色”
数据-icon=“骷髅”
viewBox=“0 32”
样式={
stylecur
} >
<
标题>头骨图标<
/a><
h1 className=“mt2 mb0巴斯克维尔一世fw1 f1”>RoboFriends<
h2 className=“mt2 mb0 f6 fw4 ttu tracked”>与您的朋友一起创建一组机器人!<
nav className=“bt bb tc mw7中心mt4”>
<
a className=“f6 f5-l链接bg动画黑色-80悬停bg最浅蓝色dib pa3 ph4-l”
href=“/”>Home<
链接到={
“/addRobo”
}
className=“导航链接”>添加机器人
<
链接到={
“/updateRobo”
}
className=“导航链接”>更新机器人
<
链接到={
“/搜索机器人”
}
className=“导航链接”>搜索机器人
<
a className=“f6 f5-l链接bg动画黑色-80悬停bg浅黄色dib pa3 ph4-l”>删除机器人
<
/导航><
/标题><
开关>{
路线图(({
身份证件
路径
组件:组件
}) => ( <
路由密钥={
身份证件
}
路径={
路径
}
组件={
组成部分
}//无尖括号
/>))
} <
/开关><
滚动>{
iSping?
<Link to="/">Home</Link>
or
<NavLink to="/">Home</NavLink>