REDUX
Ayrıca react-router-reduxhangisinin sahip olduğunu goBack()ve push().
İşte bunun için bir örnekleyici paketi:
Uygulamanızın giriş noktasında, ihtiyaç duyarsınız ConnectedRouterve bazen bağlantı kurmak için zor bir bağlantı amaçtır history. Redux ara yazılım, geçmiş değişikliklerini dinler:
import React from 'react'
import { render } from 'react-dom'
import { ApolloProvider } from 'react-apollo'
import { Provider } from 'react-redux'
import { ConnectedRouter } from 'react-router-redux'
import client from './components/apolloClient'
import store, { history } from './store'
import Routes from './Routes'
import './index.css'
render(
<ApolloProvider client={client}>
<Provider store={store}>
<ConnectedRouter history={history}>
<Routes />
</ConnectedRouter>
</Provider>
</ApolloProvider>,
document.getElementById('root'),
)
Sana bağlanmanın bir yolunu göstereceğim history. Geçmişin mağazaya nasıl aktarıldığına ve ayrıca uygulamanın giriş noktasında kullanılabilmesi için tekli olarak nasıl dışa aktarıldığına dikkat edin:
import { createStore, applyMiddleware, compose } from 'redux'
import { routerMiddleware } from 'react-router-redux'
import thunk from 'redux-thunk'
import createHistory from 'history/createBrowserHistory'
import rootReducer from './reducers'
export const history = createHistory()
const initialState = {}
const enhancers = []
const middleware = [thunk, routerMiddleware(history)]
if (process.env.NODE_ENV === 'development') {
const { devToolsExtension } = window
if (typeof devToolsExtension === 'function') {
enhancers.push(devToolsExtension())
}
}
const composedEnhancers = compose(applyMiddleware(...middleware), ...enhancers)
const store = createStore(rootReducer, initialState, composedEnhancers)
export default store
Yukarıdaki örnek blok react-router-redux, kurulum sürecini tamamlayan ara yazılım yardımcılarının nasıl yükleneceğini gösterir .
Bir sonraki bölümün tamamen ekstra olduğunu düşünüyorum, ancak gelecekte birisinin fayda görmesi durumunda onu ekleyeceğim:
import { combineReducers } from 'redux'
import { routerReducer as routing } from 'react-router-redux'
export default combineReducers({
routing, form,
})
Her routerReducerzaman kullanıyorum çünkü normalde neden olmayan Bileşenleri yeniden yüklemeye zorlamama izin veriyor shouldComponentUpdate. Bunun en bariz örneği, bir kullanıcı bir NavLinkdüğmeye bastığında güncellenmesi beklenen bir Gezinme Çubuğuna sahip olduğunuz zamandır . Bu yoldan giderseniz, Redux'un bağlantı yönteminin kullandığını öğreneceksiniz shouldComponentUpdate. İle routerReducer, mapStateToPropsyönlendirme değişikliklerini Gezinme Çubuğu'na eşlemek için kullanabilirsiniz ve bu, geçmiş nesnesi değiştiğinde güncellenmesini tetikler.
Bunun gibi:
const mapStateToProps = ({ routing }) => ({ routing })
export default connect(mapStateToProps)(Nav)
İnsanlar için fazladan anahtar sözcükler eklerken beni affedin: Bileşeniniz düzgün bir şekilde güncellenmiyorsa shouldComponentUpdate, bağlantı işlevini kaldırarak araştırın ve sorunu çözüp çözmediğine bakın. Öyleyse, çekin routerReducerve bileşen, URL değiştiğinde düzgün bir şekilde güncellenecektir.
Kapanışta, tüm bunları yaptıktan sonra, istediğiniz zaman goBack()veya push()istediğiniz zaman arayabilirsiniz !
Şimdi rastgele bir bileşende deneyin:
- İçeri aktar
connect()
- Hatta gerek yok
mapStateToPropsyamapDispatchToProps
- GoBack'te içe aktarın ve şuradan aktarın
react-router-redux
- Aramak
this.props.dispatch(goBack())
- Aramak
this.props.dispatch(push('/sandwich'))
- Olumlu duygu yaşayın
Daha fazla örneklemeye ihtiyacınız varsa şu adrese göz atın: https://www.npmjs.com/package/react-router-redux