私はreduxとreact.jsの初心者です。
別のファイル(actions/actionArticles.js)のreduxライブラリ関数に接続されている状態変数(articlesTable/index.js)にif条件を設定して、react.jsのコンポーネントでボタンを非表示にしようとしています。 、articlesTable/index.jsのボタンがクリックされると、コンポーネントはactions/actionArticles.jsに接続され、loadMoreData()と呼ばれるactions /actionArticles.jsの関数をディスパッチします。 reduxで状態を設定しようとしている関数は、
articlesActions.jsで
export const loadMoreArticles = () => async (dispatch, getState) => {
const lastArticleKey = Object.keys(getState().articlesMap).pop();
const lastArticle = getState().articlesMap[lastArticleKey];
console.log("articleMap", getState().articlesMap);
console.log("Last article", lastArticleKey, lastArticle);
let filteredArticles = {};
const uid = getState().auth.uid;
const userLevel = getState().profile.userLevel;
} else {
const filteredArticlesArray = [];
var lastArticleReached = false;
...
var lastArticleInArray = filteredArticlesArray[filteredArticlesArray.length-1];
if (lastArticleInArray[0]===lastArticleKey) {
console.log("Bingo, last article reached!");
lastArticleReached = true;
}
else if (lastArticleInArray[0]!== lastArticleKey)
{
console.log("Not last article");
lastArticleReached = false;
}
filteredArticles = Object.fromEntries(filteredArticlesArray.reverse());
}
dispatch({type: LAST_ARTICLE_REACHED, payload: lastArticleReached})
...
};
この関数をディスパッチします
dispatch({ type: LOAD_MORE_ARTICLES, payload: filteredArticles });
上記のコードスニペットで
ルートレデューサーは次のようになります。 reducers/index.js
import { combineReducers } from 'redux';
import { reducer as formReducer } from 'redux-form';
import articlesStatusReducer from './articlesStatusReducer';
const rootReducer = combineReducers({
...
articlesStatus: articlesStatusReducer,
form: formReducer,
...
});
export default rootReducer;
articleStatusReducerで、
import {LAST_ARTICLE_REACHED} from "../actions/types";
export default function(state = {}, action) {
switch(action.type) {
case(LAST_ARTICLE_REACHED):
console.log(action.payload);
return action.payload;
default:
return state;
}
}
articlesTable/index.jsでは、このように接続します
const mapStateToProps = (state) => {
return {
articlesMap: state.articlesMap,
appStatus: state.appStatus,
profile: state.profile,
lastArticleReached: state.articlesStatus,
}
};
const mapDispatchToProps = (dispatch) => {
return {
getArticlesWithData: () => dispatch(getArticlesWithData()),
loadMore: () => dispatch(loadMoreArticles())
}
};
export default compose(
withRouter,
connect(mapStateToProps, mapDispatchToProps)
)(ArticlesTable)
何らかの理由で、articleStatusが認識されません。
console.log(this.props.articleStatus)
state.articleStatus is undefined
ブール値である必要があるstate.articleStatusを参照するにはどうすればよいですか?
編集: 何らかの理由で、renderメソッドの条件付きJSXブラケットに入れると、未定義で出力されます。
render () => {
{
console.log(this.props.lastArticleReached),
!this.props.lastArticleReached
: <Button> </Button>
?
<div><div>
}
}``
回答 2 件
小道具の別の名前を試しましたが、ThomasCaillardと同様の方法で
Reducer.js
case(REACH_LAST_ARTICLE): return {...state, lastArticleReached: action.payload}
コンポーネント内index.js
const mapStateToProps = (state) => { return { ... lastArticleReached: state.articlesMap.lastArticleReached ... } };
これまでのすべての助けに感謝します
関連記事
- Reduxストアは更新されていますが、コンポーネントが再レンダリングされていません
- 反応し、constコンポーネントの子から親のconstコンポーネント変数を設定します
- 反応コンポーネントのreduxストアにデータを入力する場所とタイミング
- Matオブジェクトがデータ変数にない場合、そのデータはどこに保存されますか?
- インスタンス化したばかりのプレハブコンポーネントで、シーン上のゲームオブジェクトを参照するにはどうすればよいですか?
- まだ定義されていない関数をScalaの変数に格納することは可能ですか?
- jqueryで機能しない変数の参照
- コンポーネントへのクラス変数の変更を検出する方法
- 変数に値を格納するJavaScriptを使用した正規表現
関連した質問
- React:アクションはプレーンオブジェクトでなければなりません
- TypeError:react-reduxで未定義のプロパティ 'map'を読み取れません
- Reduxで未定義のプロパティ「タイプ」を読み取れません
- redux-toolkitのcreateSliceメソッドを使用して、さまざまなレデューサー関数で単一アクションタイプを使用する方法
- ReduxからのReact状態の更新により、最大更新深度がエラーを超えました
- React-Redux:ネストされた配列を別の配列に割り当てるアクションをディスパッチしようとしているときに無効なフック呼び出し
- javascriptプロパティが挿入されたURLをフェッチしますが、UTF-8文字がコンソールに表示され続け、404が発生します
- NextJsはサーバー側のレンダリングを介してFacebookタグをレンダリングします
- React Nativeエラー失敗したプロップタイプ:タイプ `array`の無効なプロップ` children`が `Overlay`に提供されました。
- svg要素の幅と高さを制御する方法
機能中
mapStateToProps
、マップする必要がありますstate.articleStatus
小道具に。このようなもの:
そう
this.props.articleStatus
動作します。 :)問題はレデューサーにあります。レデューサーの各ケースは状態を返す必要がありますが、あなたの場合、あなたのリターン
action.payload
。このようなことを試してください。
このように、articlesStatusは、ブール値であるarticleStatusという1つの小道具を持つオブジェクトになりました。