javascript - React/Redux 改变网页语言

标签 javascript reactjs redux

我正在制作一个具有从主屏幕更改语言功能的网站,我正在使用 Redux 来处理状态。

我不断收到 TypeError: Cannot read property 'connect' of undefined,而且它似乎来自 Home.js,我不知道为什么会发生!!请帮忙!!!

import React, { Component } from 'react';
import ReactRedux from 'react-redux';

import './Home.css';
import Navbar from '../../Reusable/Navbar/Navbar.js';
import Footer from '../../Reusable/Footer/Footer.js';
import Action from '../../Reusable/Action/Action.js';
import ProjectsCarousel from '../../Reusable/ProjectsCarousel/ProjectsCarousel.js';
import Getintouch from '../../Reusable/Getintouch/Getintouch.js';
import Header from './Header/Header.js';
import Info from './Info/Info.js';
import Whyus from './Whyus/Whyus.js';
import Testimonial from './Testimonial/Testimonial.js';
let actions = require('../../../actions');

class Home extends Component {
  render() {
        const content = this.props.content;
        const switchLanguage = this.props.switchLanguage;

        if (content){
              return (
                <div className="home">
                  <Action />
                  <Navbar data={content.page.menu} switchLanguage={switchLanguage}/>
                  <Header />
                  <Info  data={content.page.home}/>
                  <ProjectsCarousel />
                  <Whyus />
                  <Testimonial />
                  <Getintouch />
                  <Footer />
                </div>
              )
        } else {
          return;
        }

  }
}


module.exports = ReactRedux.connect(
  (state) => ({content: state.content}),
  (dispatch) => ({switchLanguage: (lang) => dispatch(actions.switchLanguage(lang))})
)(Home);

export default Home;

最佳答案

那是因为react-redux has only named exports .

将您的导入更改为:

import * as ReactRedux from 'react-redux';

这会将每个命名属性导入 ReactRedux 对象。或像这样导入:

import { connect } from 'react-redux';

export default connect(
  (state) => ({content: state.content}),
  (dispatch) => ({switchLanguage: (lang) => dispatch(actions.switchLanguage(lang))})
)(Home);

关于javascript - React/Redux 改变网页语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49597432/

相关文章:

reactjs - 组件内部调用操作 | react 、Redux

javascript - React-redux:类型错误:_this2.props.handleCityChange 不是一个函数

javascript - 如何在 AngularJS 页面中嵌套两个 Controller ?

javascript - 当事件处理程序附加到父项时,防止在某个子项之后单击

javascript - 重新创建时 knockout PureCompulated 调用订阅者

javascript - Tailwind CSS 不能用 React 和 Express 编译?

javascript - 在扩展另一个之前折叠 Accordion 分支?

reactjs - react-addons-test-utils 的新编译错误

reactjs - 使用 react-router-dom 6 在 react 中导航

javascript - 重置数组并重新渲染组件