Javascript react从ReactDOM.render调用函数
在我的index.js文件中,我有以下内容:Javascript react从ReactDOM.render调用函数,javascript,reactjs,Javascript,Reactjs,在我的index.js文件中,我有以下内容: const store = createStore( combineReducers({ i18n: i18nReducer }), applyMiddleware(thunk) ); syncTranslationWithStore(store) store.dispatch(loadTranslations(translationsObject)); store.dispatch(setLocale('de
const store = createStore(
combineReducers({
i18n: i18nReducer
}),
applyMiddleware(thunk)
);
syncTranslationWithStore(store)
store.dispatch(loadTranslations(translationsObject));
store.dispatch(setLocale('de'));
使用此代码,默认语言设置为greman。现在,我想在ReactDOM.render中创建一个按钮,在这里我可以更改语言,最好使用stahet(de/en)
我所做的是:
function changeLanguage() {
function handleClick(e) {
e.preventDefault();
store.dispatch(setLocale('de'));
}
}
而反应域中的
<Router history={hashHistory}>
<div>
<div className="sidebararound">
<div className="sidebar">
<ul>
<li><a className="fa fa-language fa-2x" onClick={this.changeLanguage.handleClick.bind()} aria-hidden="true"></a></li>
但这不起作用。我有一张白纸。我的错在哪里
谢谢在构造函数中绑定事件始终是一种很好的做法
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this)
....
}
handleClick(e){
e.preventDefault();
store.dispatch(setLocale('de'));
}
在渲染方法中:
<Router history={hashHistory}>
<div>
<div className="sidebararound">
<div className="sidebar">
<ul>
<li><a className="fa fa-language fa-2x" onClick={this.handleClick} aria-hidden="true"></a></li>
在构造函数中绑定事件始终是一种很好的做法
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this)
....
}
handleClick(e){
e.preventDefault();
store.dispatch(setLocale('de'));
}
在渲染方法中:
<Router history={hashHistory}>
<div>
<div className="sidebararound">
<div className="sidebar">
<ul>
<li><a className="fa fa-language fa-2x" onClick={this.handleClick} aria-hidden="true"></a></li>
我认为问题出在这里this.changeLanguage.handleClick.bind()
,changeLanguage不是一个对象,尝试使用函数handleClick(e){e.preventDefault();store.dispatch(setLocale('de');}
和onClick={this.handleClick.bind(this)}
关闭。谢谢功能正常。但是我必须将onClick事件更改为:onClick={handleClick}
我认为问题出在这里this.changeLanguage.handleClick.bind()
,changeLanguage不是对象,请尝试使用函数handleClick(e){e.preventDefault();store.dispatch(setLocale('de');}
和onClick={this.handleClick.bind(this)}
其关闭。谢谢。函数可以。但我必须将onClick事件更改为:onClick={handleClick}