Javascript MobX存储未在React Native中更新

Javascript MobX存储未在React Native中更新,javascript,reactjs,react-native,redux,mobx,Javascript,Reactjs,React Native,Redux,Mobx,我在我的React原生应用程序中实现了一个MobX商店,以跟踪用户是否被跟踪。follow/unfollow正在注册,但MobX存储未更新。我正试图用this.follows.items[index]={…user,isFollowing:!user.isFollowing}直接更新它,但由于某种原因,存储区没有触发更新 这是视图组件 @observer class FollowsListView extends Component<Props> { follows =

我在我的React原生应用程序中实现了一个MobX商店,以跟踪用户是否被跟踪。follow/unfollow正在注册,但MobX存储未更新。我正试图用
this.follows.items[index]={…user,isFollowing:!user.isFollowing}
直接更新它,但由于某种原因,存储区没有触发更新

这是
视图
组件

@observer
class FollowsListView extends Component<Props> {
  follows =
    this.props.followType === 'followers'
      ? followsStore.getFollowersListLoader(this.props.userId)
      : followsStore.getFollowingListLoader(this.props.userId);

  componentDidMount = () => {
    this.follows.lazyLoad();
  };

  render() {
    return (
      <>
        <AppHeader
          title={
            this.props.followType === 'followers' ? 'FOLLOWERS' : 'FOLLOWING'
          }
        />
        <FlatList
          contentContainerStyle={{ padding: 15 }}
          data={this.follows.items}
          keyExtractor={this.getId}
          onEndReached={this.follows.loadMore}
          onEndReachedThreshold={0.2}
          onRefresh={this.follows.loadFromStart}
          refreshing={this.follows.isLoading}
          renderItem={this.renderFollows}
        />
      </>
    );
  }

  private getId = (user: { id: string }) => user.id;

  renderUserActionButton(user: UserContainer) {
    console.log(user);
    return (
      user.id !== _SessionManager.id && (
        <TouchableOpacity
          onPress={() => this.openActionMenu(user.following || user.owner)}
        >
          <Image source={Images.moreDots} />
        </TouchableOpacity>
      )
    );
  }

  openActionMenu(user: User) {
    const name = user.name || user.username;

    const followOption = { name: 'follow', title: `Follow @${name}` };
    const unfollowOption = { name: 'unfollow', title: `Unfollow @${name}` };

    const options = {
      customButtons: [user.isFollowing ? unfollowOption : followOption],
      title: null,
      takePhotoButtonTitle: null,
      chooseFromLibraryButtonTitle: null,
    };

    ImagePicker.showImagePicker(options, ({ customButton }) => {
      if (customButton === 'follow') {
        this.props.changeIsFollowingUser(user.id, false);
      }
      if (customButton === 'unfollow') {
        this.props.changeIsFollowingUser(user.id, true);
      }

      const index = this.follows.items.findIndex((user) => user.id);
      this.follows.items[index] = { ...user, isFollowing: !user.isFollowing };
    });
  }

  private renderFollows: ListRenderItem<UserContainer> = ({ item: user }) => {
    const userId = user.following ? user.following.id : user.id;

    return (
      <UserRow
        actionContent={this.renderUserActionButton(user)}
        onPress={() => this.props.navigateTo('ProfilePublic', { userId })}
        user={user.following || user.owner}
      />
    );
  };
}

const mapDispatchToProps = (dispatch: Function): MappedDispatch =>
  bindActionCreators(
    {
      changeIsFollowingUser,
      navigateTo,
    },
    dispatch
  );

export default connect(
  null,
  mapDispatchToProps
)(FollowsListView);

在MobX中,为了更改状态,您需要使用一个。将您的
openActionMenu
设置为一个操作或将状态更改代码提取到另一个函数,并将其作为操作进行修饰,使其更干净

嘿@Akrion谢谢你的回复。我尝试了
@action
followChangeAction=(user:user)=>{const index=this.follows.items.findIndex((user)=>user.id);this.follows.items[index]={…user,isFollowing:!user.isFollowing};};但是仍然没有运气。在实际存储中设置@action并在代码中调用它。
import ListLoader from 'Network/ListLoader';
import { Follows } from 'Follows/Types';
import _API from 'Network/API';

class FollowsStore {
  followers = new Map<string, Follows>();
  followersList = new Map<string, ListLoader<Follows>>();
  following = new Map<string, Follows>();
  followingList = new Map<string, ListLoader<Follows>>();

  getFollowersListLoader(userId: string) {
    const list = this.followersList.get(userId);
    if (list) return list;

    const newList = new ListLoader<Follows>({
      fetchData: async (params) => {
        const url = `users/${userId}/followers`;
        const response = await _API.get(url, { params });
        return response.data;
      },
      onLoad: (data) => {
        for (const user of data.items) {
          this.followers.set(user.id, user);
        }
      },
    });

    this.followersList.set(userId, newList);
    return newList;
  }

  getFollowingListLoader(userId: string) {
    const list = this.followingList.get(userId);
    if (list) return list;

    const newList = new ListLoader<Follows>({
      fetchData: async (params) => {
        const url = `users/${userId}/following`;
        const response = await _API.get(url, { params });
        return response.data;
      },
      onLoad: (data) => {
        for (const user of data.items) {
          this.following.set(user.id, user);
        }
      },
    });

    this.followingList.set(userId, newList);

    console.log(newList);
    return newList;
  }
}

const followsStore = new FollowsStore();

export default followsStore;