首页 > 解决方案 > Redux 连接的组件不渲染

问题描述

我是 redux 的新手,目前正在尝试让连接的组件渲染,但没有渲染。Main.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;

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

标签: reactjsreduxreact-reduxreact-routerreact-router-redux

解决方案


我认为您的代码的唯一问题是您应该将组件放在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>


推荐阅读