V
V
vaskadogana2017-02-09 18:35:21
React
vaskadogana, 2017-02-09 18:35:21

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

2 answer(s)
F
Frozen Coder, 2017-02-09
@frozen_coder

I need this setLanguage() function to work,
I can’t figure out how to make it work by updating state.language

Examine the lifecycle of a component. If I understand correctly. You need the component's componentWillReceiveProps(nextProps) method - write down everything that happens when new props arrive. the new language will come to you in nextProps when it changes in the Store, because you did connect.
Make your functions class methods, otherwise they are somehow divorced from the component, although they use its state. Look towards more modern syntax in React and ES6, at least var is no longer comme il faut)
https://facebook.github.io/react/docs/react-compon...

V
vaskadogana, 2017-02-10
@vaskadogana

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 question

Ask a Question

731 491 924 answers to any question