如果我理解正確,提供程序組件/方法允許以redux存儲訪問權限,而不必直接在每個組件內導入它。假設我的理解是正確的,那麼我想知道爲什麼我無法在App.js中獲得store.getState()。它說,店裏是未定義....React獲取狀態提供者方法
Index.js
const store = createStore(allReducers,{})
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root'));
App.Js
class App extends Component {
render() {
console.log(store.getState())
return (
<div className="App">
<div className="individual">
<House/>
<NameTag/>
</div>
</div>
);
}
}
但是,請注意,您**不應**通常以這種方式直接訪問商店。使用React-Redux'connect()'函數來創建包裝器組件,以替代地管理商店訂閱。 – markerikson
@markerikson我同意。如果我們必須從存儲中訪問一些值,那麼我們應該使用connect()函數來創建一個包裝器。但是我遇到了一種情況,我需要在共享同一商店的單個應用程序中處理兩個提供程序組件。在這種情況下,我使用上下文類型來獲得商店。 –
呃......沒有道理。你是否說_two_ Redux Providers纏繞在一起?還是一個Redux Provider和一個與其他庫相關的組件? – markerikson