Redux 连接的组件不渲染
Posted
技术标签:
【中文标题】Redux 连接的组件不渲染【英文标题】:Redux connected component does not render 【发布时间】:2021-02-03 15:04:46 【问题描述】:我是 redux 的新手,目前正在尝试让连接的组件渲染,但没有渲染。 Main.js 中的标头标签应该呈现,但它没有。我真的不明白问题是什么,没有任何语法错误或编译问题。这是我的代码:
App.js:
import bindActionCreators from 'redux';
import connect from 'react-redux';
import Main from './Main';
function mapStateToProps(state)
return
//state goes here
// eg: const actionCreators = ...allStudentActions;
function mapDispatchToProps(dispatch)
// return bindActionCreators(actionCreators, dispatch);
return;
const App = connect(mapStateToProps, mapDispatchToProps)(Main);
export default App;
Main.js:
import React from 'react';
class Main extends React.Component
render()
return (
<div>
<h1>PlaceMint</h1>
React.cloneElement(...this.props.children, ...this.props)
</div>
)
export default Main;
index.js
import React from 'react';
import render from 'react-dom';
import App from './components/App';
// import pages components
// router dependencies
import Route, Switch from 'react-router';
import BrowserRouter from 'react-router-dom';
import Provider from 'react-redux';
import store from './store';
const router = (
<Provider store=store>
<BrowserRouter>
<Route path="/" component=App>
<Switch>
/* All the pages go here, wrapperd in react router 'Route tags' see react router docs */
</Switch>
</Route>
</BrowserRouter>
</Provider>
)
render(router, document.getElementById('root'));
【问题讨论】:
【参考方案1】:我认为您的代码的唯一问题是您应该将 Route
放在 Switch
组件内,这样它只会为匹配的路由呈现一个组件。
可能您的空Switch
是问题所在,因为您的路线中没有匹配的组件。移动 Switch
或将您的路线包裹在其中 - 取决于您要创建的内容。
Switch 用于仅渲染第一个匹配的路由。更多详情请关注here。
您还没有发布您的商店定义,所以我在下面的演示中或在下面的codesandbox 中创建了一个简单的示例商店。
注意:以下代码仅供参考。我无法让它在这里运行。
/*
// import pages components
import bindActionCreators from "redux";
import connect from "react-redux";
// router dependencies
import Route, Switch from "react-router";
import BrowserRouter from "react-router-dom";
import Provider from "react-redux";
import createStore from "redux";
import React from "react";*/
class Main extends React.Component
render()
const greeting = this.props;
console.log(greeting);
return (
<div>
<h1>PlaceMint</h1>
greeting
/*React.cloneElement( ...this.props .children, ...this.props )*/
</div>
);
function mapStateToProps(state)
return
greeting: state.greeting
//state goes here
;
// eg: const actionCreators = ...allStudentActions;
function mapDispatchToProps(dispatch)
// return bindActionCreators(actionCreators, dispatch);
return
dispatch
;
const App = connect(mapStateToProps, mapDispatchToProps)(Main);
const initialState =
greeting: "hello from redux"
;
//import store from "./store";
const rootReducer = (state = initialState, action) =>
return state;
;
const store = createStore(rootReducer);
const router = (
<Provider store=store>
<BrowserRouter>
<Switch>
<Route path="/" component=App />
/* All the pages go here, wrapperd in react router 'Route tags' see react router docs */
</Switch>
</BrowserRouter>
</Provider>
);
render(router, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/4.0.1/redux.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-redux/6.0.1/react-redux.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>
You need to enable javascript to run this app.
</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
【讨论】:
这可能是一个愚蠢的问题,但是你为什么要注释掉 Main.js 中的 React.cloneElement 行;根据我对正在关注的教程的理解,它允许 App.js 下的所有路由子节点访问状态和道具,而无需在需要访问 redux 存储的每个子元素中使用 mapDispatch 和 mapState。 我评论了它,因为我收到了一个错误React.cloneElement(...): The argument must be a React element, but you passed undefined.
。你能分享教程的链接让我看看吗?评论它以获得一个工作示例。
哦,Switch
的东西不对。没关系,就像你有它一样。您甚至可以将其删除以进行测试。但后来我在您的代码中看不到问题 - 也许它在您的 store
文件或 React.cloneElement
东西中。
这是 WesBos 的教程视频 (youtube.com/…) 的链接;非常感谢。
我现在明白了。你的开关在Route
里面,这是个问题。将其放在Route
下方或周围。以上是关于Redux 连接的组件不渲染的主要内容,如果未能解决你的问题,请参考以下文章