F
F
ff0xff2018-03-31 10:44:54
React
ff0xff, 2018-03-31 10:44:54

Simple example of updating store in reactjs redux via ajax?

Good time of the day, please tell me, the simplest example on reactjs for updating stora via ajax, that would be the simplest and most understandable ... That would be so simple that 1 component 1 storage 1 request. in google it is possible to find only very difficult examples.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2018-03-31
@ff0xff

Store - not in react, it is in redux.
In react, you need to update the state so that the new data gets into the component, and it is updated.
Look here https://youtu.be/fsC6r_prek8?t=51m30s
Source code
Essence: an xhr request is made, after which new data is set in the state.
Pseudo code:

// ваш компонент

// 1) устанавливаем начальный state
state = {
 data: []
}

// 2) делаем ajax (xhr) запрос, например в comonentDidMount (в момент, когда компонент примонтирован)

componentDidMount() {
   // $.ajax( ...
   // или xhr,  или fetch 
   // покажу fetch
   const self = this
   fetch(`${API_ROOT}/${endPoint}`)
    .then(ОТВЕТ => {
        // УСТАНАВЛИВАЕМ в STATE
        self.setState({ data: ОТВЕТ })
      } 
    })
 // реальный код фетча - https://github.com/maxfarseer/forge/blob/2-books-router/src/helpers/network.js
}

// 3) в компоненте рисуем

render() { return <div>ваши_данные</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question