Answer the question
In order to leave comments, you need to log in
Using redux and store, how to trigger event on store state change?
you need to execute the following chain event > action > dispatch store > ?? > event > rerender
Where there are question marks, I don’t understand how to implement
the code, I’ll try to make it as clean as possible
<form className="autorization_form">
class Languages extends React.Component{
render() {
return (
<input
defaultValue = "ua"
onClick = {this.props.languageUpdate}
/>
)
}
};
var EAction = {
LANGUAGE_UPDATE : "LANGUAGE_UPDATE"
}
function reducer(state = { language : ""}, action) {
case EAction.LANGUAGE_UPDATE:
return {
...state,
language: action.language,
};
default:
return state;
}
}
function languageUpdate(event){
return {
type : EAction.LANGUAGE_UPDATE,
language : event.target.value,
}
}
var LanguagesController = connect(
state =>({
language : state.language
}),
dispatch => bindActionCreators({
languageUpdate,
}, dispatch)
)(Languages, FormAuthView)
var store = createStore(reducer, applyMiddleware(thunk));
мне нужно чтобы отработала эта функция setLanguage(),
не могу понять как заставить ее отработать по обновлению state.language</b>
function setLanguage(state){
state = store.getState();
var lang = state.language;
language_variant.setLanguage(lang);
alert('languge was setup');
};
и произошел ререндер этого компонента ну или вообще любого (но это уже следующий вопрос))))
export default class Form extends React.Component{
render(){
return(
<div>
<div id="form_container">
<Provider store = {store}>
<FormAuthController/>
</Provider>
</div>
<div className="languages_icon">
<Provider store = {store}>
<LanguagesController />
</Provider>
</div>
</div>
)
}
}
Answer the question
In order to leave comments, you need to log in
I need this setLanguage() function to work,
I can’t figure out how to make it work by updating state.language
to perform some event to update the redux store, use the subscribe()
function store.subscribe(() =>
console.log(store.getState()) - the event that should happen
)
store - the name of the store
I will look further for how to initiate the rerender of the component
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question