import {
SEARCH_CHAT_FROM_RECENT_CHAT_CONTAT_LIST,
GET_RECENT_CHAT_CONTAT_LIST_REQUEST,
GET_RECENT_CHAT_CONTAT_LIST_SUCCESS,
GET_RECENT_CHAT_CONTAT_LIST_FAILURE
} from "../actions/action-types";
const INTIAL_STATE = {
response: null,
error: null,
loading: false,
searchResults: null,
};
searchChatFromRecentChatContactList = (state, text) => {
if(state.response && state.response.length > 0) {
const response = [...state.response];
const searchResults = response.filter(item => item.displayName.includes(text));
return searchResults;
}
return [];
}
export default (state = INTIAL_STATE, action) => {
switch (action.type) {
case GET_RECENT_CHAT_CONTAT_LIST_REQUEST:
return { ...state, loading: true, response: null, error: null, };
case GET_RECENT_CHAT_CONTAT_LIST_SUCCESS:
return { ...state, response: action.payload, loading: false};
case GET_RECENT_CHAT_CONTAT_LIST_FAILURE:
return { ...state, response: null, error: action.payload, loading: false };
case SEARCH_CHAT_FROM_RECENT_CHAT_CONTAT_LIST:
return {...state, searchResults: searchChatFromRecentChatContactList(state, action.payload)};
default:
return state;
}
};
を使用していたとき、私は私のstate.response
に文字列の配列を持っていますが、何らかの理由で私の下の方法は、常に、[]を返して期待される出力を与えていません。Array.prototype.filter()私は私の減速に
state.response = [{displayName: 'someText'}, {displayName: 'someText otherText'];
入力:
searchChatFromRecentChatContactList(state, 'SomeText')
出力:
[];
はあなたに渡しているテキストの値をチェック返しますsearchChatFromRecentChatContactList関数 – stack26
ありがとう@ stack26 :-) –