javascript - react-redux-i18n 切换语言

标签 javascript reactjs redux internationalization jsx

我遵循了这个库的自述文件:url . 一切都很好,除了一件事:我怎么能从内部切换语言,例如,我的组件?

我尝试在我的 NavBar 组件中使用一个 Action ,但它返回给我“this.props.dispatch”不是控制台中的函数。

这是我的组件:

import React from 'react';
import { Link } from 'react-router';
import { connect } from 'react-redux';
import { logout } from '../actions/authActions';

import { Translate, Localize } from 'react-redux-i18n';
import { loadTranslations, setLocale, syncTranslationWithStore } from 'react-redux-i18n';

class NavigationBar extends React.Component {
    constructor(props){
        super(props);

        this.changeLang = this.changeLang.bind(this);
    }

    logout(e) {
        e.preventDefault();
        this.props.logout();
    }

    changeLang(){
        this.props.dispatch(setLocale('nl'));
    }

    render() {
        const { isAuthenticated } = this.props.auth;
        const userLinks =  (
            <a href="#" onClick={ this.logout.bind(this) }>Logout</a>
        );

        const guestLinks =  (
            <div>
                <Link to="/signup">Signup</Link>
                <Link to="/login">Login</Link>
            </div>
        );

        return (
            <div>
                { isAuthenticated ? userLinks: guestLinks }
                <Translate value="application.title"/>
                <br />
                <a href="#" onClick={this.changeLang}>NL</a>
            </div>
        );
    }
}

NavigationBar.propTypes = {
    auth: React.PropTypes.object.isRequired,
    logout: React.PropTypes.func.isRequired
}

function mapStateToProps(state){
    return {
        auth: state.auth
    };
}

export default connect(mapStateToProps, { logout })(NavigationBar);

最佳答案

虽然这有点老了,但这种方法可能对其他人有帮助:

您可以使用“redux”中的 bindActionCreators 来映射组件中所需的所有操作,而不必担心显式使用 dispatch()

import {bindActionCreators} from 'redux';

// class definition ...

const mapDispatchToProps = (dispatch) => {
    const actions = {
        setLocale,
        // ... you can specify other actions that need dispatch here
    };
    return {
       actions: bindActionCreators(actions, dispatch) // this will set the 'actions' property to props
    }
};

function mapStateToProps(state){
    return {
        auth: state.auth
    };
}

const connectedNavigationBar = connect(
    mapStateToProps,
    mapDispatchToProps
)(NavigationBar);

export default connectedNavigationBar;

现在您可以像这样调用函数:this.props.actions.setLocale('nl');

您的代码将如下所示:

import React from 'react';
import { Link } from 'react-router';
import { connect } from 'react-redux';
import { logout } from '../actions/authActions';
import { bindActionCreators } from 'redux';

import { Translate, Localize } from 'react-redux-i18n';
import { loadTranslations, setLocale, syncTranslationWithStore } from 'react-redux-i18n';

class NavigationBar extends React.Component {
    constructor(props){
        super(props);

        this.changeLang = this.changeLang.bind(this);
    }

    logout(e) {
        e.preventDefault();
        this.props.logout();
    }

    changeLang(){
        // changed from 
        // this.props.dispatch(setLocale('nl'));
        this.props.actions.setLocale('nl');
    }

    render() {
        const { isAuthenticated } = this.props.auth;
        const userLinks =  (
            Logout
        );

        const guestLinks =  (

                Signup
                Login

        );

        return (

                { isAuthenticated ? userLinks: guestLinks }



                NL

        );
    }
}

NavigationBar.propTypes = {
    auth: React.PropTypes.object.isRequired,
    logout: React.PropTypes.func.isRequired
}

function mapStateToProps(state){
    return {
        auth: state.auth
    };
}

const mapDispatchToProps = (dispatch) => {
    const actions = {
        setLocale,
        // ... you can specify other actions that need dispatch here
    };
    return {
       actions: bindActionCreators(actions, dispatch) // this will set the 'actions' property to props
    }
};

const connectedNavigationBar = connect(
    mapStateToProps,
    mapDispatchToProps
)(NavigationBar);


export default connectedNavigationBar;

可以查看this进一步阅读。

关于javascript - react-redux-i18n 切换语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39661119/

相关文章:

javascript - 如何在检查同级输入时禁用输入?

javascript - 如何访问reactjs中另一个组件的功能?

javascript - 在带有异步调度的react-native中使用redux-thunk

reactjs - Redux/React - 为什么我无法将状态绑定(bind)到 iframe 中组件上的 this.props?

reactjs - 什么是 "universal app"?

javascript - BrowserAuthError : interaction_in_progress - Unable to fix, 无论找到什么解决方案

javascript - Grails:选择另一个下拉列表时填充下拉列表

javascript - 如何关闭带有链接或按钮等的html页面?

reactjs - 无法在 React 路由器中正确添加前缀到路径

javascript - 处理硬件后退按钮点击