我是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'));
答案 0 :(得分:1)
我认为您的代码唯一的问题是,您应该将Route
放在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>