React / redux: In which lifecycle methods is this allowed / forbidden / frowned / encouraged to send actions? What for?

In which of the responsive life support methods ( https://facebook.imtqy.com/react/docs/react-component.html ) is it allowed / forbidden / recommended / not recommended to send reduction actions? Why?

Say my stateless component uses data downloaded from the server, is it ok to send during the render()action to schedule an ajax call?

+4
source share
3 answers

The answer is stubborn, but he did not approve at all: life cycle methods are called in response to a change in the state of the component tree, but sending an action is likely to result in another state change. This is a situation with a cascading update of a handler event, which allows consux to be reduced: you must make state changes in response to actions and nothing more, otherwise you will have these reactive loops. A change in state occurs immediately.

In practice, this rule often relaxes for convenience. So there is no real correct answer! Like most things in software, it is nuanced.

, ( - ) : , , " " " , ", constructor componentWillMount. , , , componentDidMount/componentWillUpdate/componentDidUpdate.

+1

render(). :

render() , , , . , componentDidMount() . render() .

, , , .. componentDidMount(). :

componentDidMount() . , DOM, . , . .

props state , componentWillReceiveProps() componentWillUpdate() .

+4

ajax , componentDidMount, facebook, If you need to load data from a remote endpoint, this is a good place to instantiate the network request.

, , , ajax . , React, ajax- componentDidMount, .

redux , , .

+3

Source: https://habr.com/ru/post/1680134/


All Articles