简体   繁体   English

Redux 连接组件不渲染

[英]Redux connected component does not render

I'm new to redux, and currently trying to get a connected component to render, yet nothing renders.我是 redux 的新手,目前正在尝试让连接的组件进行渲染,但没有任何渲染。 The header tag in Main.js should render, yet it doesn't. Main.js 中的 header 标签应该呈现,但它没有。 I really don't get what the problem is, there aren't any syntax errors, or compilation problems.我真的不明白问题是什么,没有任何语法错误或编译问题。 Here's my code:这是我的代码:

App.js:应用程序.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:主要.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索引.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'));

I think the only problem with your code is that you should place the Route inside of Switch component so it will only render one component for the matching route.我认为您的代码唯一的问题是您应该将Route放在Switch组件中,这样它只会为匹配的路由呈现一个组件。

Probably your empty Switch is the problem as there is no matching component inside your route.可能您的空Switch是问题所在,因为您的路线中没有匹配的组件。 Move the Switch or wrap your route inside it - depends on what you're trying to create.移动Switch或将您的路线包裹在其中 - 取决于您要创建的内容。

Switch is used to only render the first matching route. Switch 仅用于渲染第一个匹配的路由。 For more details, please have a looke here .欲知更多详情,请看这里

You haven't posted your store definition so I've created a simple example store in the demo below or in the following codesandbox .您尚未发布您的商店定义,因此我在下面的演示或以下代码和框中创建了一个简单的示例商店。

Note: The code below is just for reference.注:以下代码仅供参考。 I couldn't get it to run here.我无法让它在这里运行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM