Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
React Redux re-fetch data using state and state change

I would like to pass data from my state to a fetch call (e.g. search term). When the state changes, it should re-fetch my data.

The lifecycle methods seem to be the place to do this, but when I use them it either does nothing (render is called before state change), or it goes on an endless loop.

Here is my simplified code:

@connect((store) => {
    return {
        collections : store.collections.collections
    }
}, {
    fetchCollections
})
export default class CollectionPane extends Component {

    constructor(props){
        super(props);

        this.state = {
           term : null
        }
    }

    componentWillMount(){
        this.handleFetchCollections(this.state.term);   // This loads initial ok
    }

    componentDidUpdate(){
        // this.handleFetchCollections(this.state.term);  // This causes loop
    }

    handleFetchCollections(props, sort){
        log('Fetching...', 'green');
        this.props.fetchCollections(props, sort);
    }

    onSearch(){
		
		const term = "test example";
		
		this.setState({
			term
		})
    }

    render(){
        const { collections } = this.props;

        return (
            <div>
                <a style={{ display: "block", padding: "1em" }} onClick={this.onSearch.bind(this)}>Search</a>

                <CollectionList collections={collections} />
            </div>
        );
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.2/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.2/react-dom.min.js"></script>

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You should compare your new state with the previous state before performing fetch:

componentDidUpdate(prevProps, prevState) {
    if (prevState.term !== this.state.term) {
        this.handleFetchCollections(this.state.term);
    }
}
over 4 years ago · Santiago Trujillo Denunciar

0

With react-redux you should change your state through actions and reducers. Then you can use async actions as described here: http://redux.js.org/docs/advanced/AsyncActions.html

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda