Redux连接的组件不呈现



我是redux的新手,目前正在尝试渲染一个连接的组件,但没有任何渲染。Main.js中的header标记应该呈现,但它没有。我真的不明白问题是什么,没有任何语法错误或编译问题。这是我的代码:

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'));

我认为代码的唯一问题是应该将Route放置在Switch组件内部,这样它只会为匹配的路由呈现一个组件。

可能你的空Switch是问题所在,因为你的路线内没有匹配的组件。移动Switch或将您的路线包裹在其中——这取决于您试图创建的内容。

Switch仅用于渲染第一个匹配的路由。有关更多详细信息,请查看此处。

你还没有发布你的商店定义,所以我在下面的演示或下面的代码沙盒中创建了一个简单的示例商店。

注意:下面的代码仅供参考。我无法让它在这里运行。

/*
// 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>

相关内容

  • 没有找到相关文章

最新更新