![](/img/trans.png)
[英]React / Redux / Reselect - is mapped state to props using selectors synchronous or asynchronous?
[英]React Native Redux Using state props and selectors together
在我的 react-native redux 應用程序中,在我的mapStateToProps
函數中,我需要組合來自 state 的一些元素和一些使用 reselect 創建的選擇器。
基本上,我有兩件事:
const mapStateToProps = (state) => ({
storedUserName: state.user.name,
isUserLoggedIn: state.user.isLoggedIn,
})
const mapStateToProps = createStructuredSelector({
email: emailSelector,
userData: userDataSelector
});
我需要一種方法將這兩者結合起來,以便我擁有所有 4 個道具,並且只有一個mapStateToProps
函數。 我只是不知道該怎么做。 請幫忙。
試試這個代碼
import { createSelector } from 'reselect'
const emailSel = state => state.user.email
const userDataSel = state => state.user
const emailSelector = createSelector(
emailSel,
email => email // you can make calculations on input and return value
)
const userDataSelector = createSelector(
userDataSel,
userData => userData // you can make calculations on input and return value
)
const mapStateToProps = (state) => ({
storedUserName: state.user.name,
isUserLoggedIn: state.user.isLoggedIn,
email: emailSelector,
userData: userDataSelector
})
選擇器:選擇器是將 Redux 狀態作為參數並返回一些數據的函數
reselect :當您計算派生數據/進行計算/對選擇器選擇的數據應用過濾器時,您將使用重新選擇庫。 重新選擇是有效的。
注意:當您只需要從 state 獲取數據而不需要任何計算和過濾器時,您不需要 reslsect
選擇器是將 Redux 狀態作為參數並返回一些數據以傳遞給組件的函數,如下所示:
// selector
const getDataType = state => state.editor.dataType;
你可以做的是
import { getDataType } from '../selectors'
const mapStateToProps = (state) => ({
storedUserName: state.user.name,
isUserLoggedIn: state.user.isLoggedIn,
dataType: getDataType(state) <-- selector -->
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.