[英]Why redux doesn't change state?
我正在使用 Redux 構建一個簡單的 React Native 應用程序。
這是我最初的 state
import { createStore } from "redux";
import reducer from "../reducers";
const initialState = {
user: {
uuid: null,
bio: null,
gender: null,
full_name: null,
cover_photo: null,
profile_photo: null
},
followers: {
count: 0,
list: null
},
};
export const store = createStore(reducer, initialState);
行動
export const fetch_followers = (resp) => {
return {
type: FETCH_FOLLOWERS_SUCCESS,
payload: resp
}
}
減速器
import {
FETCH_FOLLOWERS_SUCCESS
} from '../actions';
export default (state, action) => {
switch(action.type){
case FETCH_FOLLOWERS_SUCCESS:
return {
...state,
followers: {
count: action.payload.profiles.length,
list: action.payload.profiles
}
}
default:
return state
}
}
api 包裝
class Client {
constructor() {
this.endpoints = {
FETCH_MY_FOLLOWERS: 'https://run.mocky.io/v3/3d60da9d-1dd5-4e10-a69c-f2eefaa9a5da',
FETCH_I_AM_FOLLOWING: 'https://run.mocky.io/v3/3d60da9d-1dd5-4e10-a69c-f2eefaa9a5da',
}
}
followers() {
return fetch(this.endpoints.FETCH_MY_FOLLOWERS).then(response => response.json());
}
}
const client = new Client()
export default client;
我的視圖組件
import React, { Component } from 'react';
import {SafeAreaView, ScrollView} from 'react-native';
import Follower from '../components/Follower';
import client from '../helpers/rabonia_sdk/RaboniaClient';
import { fetch_followers } from '../actions';
import { store } from '../store';
export default class Follow extends Component {
constructor(){
super()
}
componentDidMount() {
client.followers().then(resp => {
if(resp.status === "OK") {
store.dispatch(fetch_followers(resp))
}
});
}
render() {
return (
<SafeAreaView>
<ScrollView>
{store.getState().followers.list.map(follower => <Follower follower={follower} /> )}
</ScrollView>
</SafeAreaView>
);
}
}
那一個是模擬 api 響應
{
"status": "OK",
"message": "Fetched",
"profiles": [
{"uuid": "a72ef52a-b814-4bc8-85ee-263621a6d96d", "full_name": "Michael Ballack", "profile_photo": "https://picsum.photos/200" },
{"uuid": "a72ef52a-b814-4bc8-85ee-263621a6d96d", "full_name": "Patrice Evra", "profile_photo": "https://picsum.photos/200" },
{"uuid": "a72ef52a-b814-4bc8-85ee-263621a6d96d", "full_name": "Dennis Bergkamp", "profile_photo": "https://picsum.photos/200" },
{"uuid": "a72ef52a-b814-4bc8-85ee-263621a6d96d", "full_name": "Michael Owen", "profile_photo": "https://picsum.photos/200" }
]
}
我對其他組件具有相同的結構,並且它們可以正常工作。
When I switch to local state (see below) it works, but can not determine what is problem with dispatching api payload to redux state?
export default class Follow extends Component {
constructor(){
super()
this.state = {
followers: []
}
}
componentDidMount() {
client.followers().then(resp => {
if(resp.status === "OK") {
this.setState({followers: resp.profiles});
store.dispatch(fetch_followers(resp))
}
});
}
render() {
let collection = store.getState().followers.list || this.state.followers
return (
<SafeAreaView>
<ScrollView>
{collection.map(follower => <Follower follower={follower} /> )}
</ScrollView>
</SafeAreaView>
);
}
}
在 reducer 文件中,您必須為其分配您在頂部聲明的 initialState
export default (state = initialState , action) => {
switch(action.type){
case FETCH_FOLLOWERS_SUCCESS:
return {
...state,
followers: {
count: action.payload.profiles.length,
list: action.payload.profiles
}
}
default:
return state;
}
}
Follow
組件沒有通過 HOC 或 useSelector connected
到 Redux。
文檔在這里
例子:
const mapStateToProps = (state) => ({
followers: getFollowersListSelector(state),
})
export default connect(
mapStateToProps,
{}
)(Follow)
選擇器:
export const getFollowersSelector = state => state.followers
export const getFollowersListSelector = state => getFollowersSelector(state).list
並在render
中使用它:
this.props.followers.map(...)
旁注:您可以使用 function 組件和 React & Redux Hooks 將代碼保持在最低限度並使代碼更具可讀性。
我對其他組件具有相同的結構,並且它們可以正常工作。
在沒有看到其他組件的情況下,不清楚您的確切意思是什么,但您的商店未在此處連接到您的組件,這就是為什么當 redux state 更新時組件不會重新渲染的原因。
請參閱此處的connect()
文檔: https://react-redux.js.org/api/connect
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.