【发布时间】:2020-12-07 08:27:51
【问题描述】:
我正在使用 Redux 构建一个简单的 React Native 应用程序。
这是我的初始状态
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" }
]
}
其他组件的结构相同,它们可以正常工作。
当我切换到本地状态时(见下文),它可以工作,但无法确定将 api 有效负载分派到 redux 状态有什么问题?
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>
);
}
}
【问题讨论】:
标签: javascript react-native redux