繁体   English   中英

将 redux state 复制到组件 state 是优化反应应用程序的好方法吗?

[英]Copying redux state to component state is good way to optimize react app?

When I need to change app state I copy part of redux state to component state and work with local state like this

export class User extends React.Component {
  state = {
    user: this.props.user,
  };

  render() {
    return (
      <div>
        <input
          value={this.state.user.name}
          onChange={this.changeUserName}
        />
        <button onClick={this.saveUser}>Save</button>
      </div>
    );
  }

  userNameChange = ({target}) => {
    this.setState((prevState) => ({
      user: {
        ...prevState.user,
        name: target.value,
      },
    }));
  }

  saveUser = () => {
    const {
      userActions,
    } = this.props;

    const {
      user,
    } = this.state;

    userActions.save(user);
  }
}

const mapStateToUserProps = (state) => ({
  user: state.user,
});

const mapDispatchToUserProps = (dispatch) => ({
  userActions: bindActionCreators(UserActions, dispatch),
})

export const UserContainer = connect(mapStateToUserProps, mapDispatchToUserProps)(User);

这将防止调用 redux 选择器。 这是优化应用程序的好方法还是应该只对减速器进行所有更改?

我相信没有太大的不同,因为最后你想保存/编辑你的数据,所以你需要在最后发送一个request

创建请求较少的应用程序更为重要,如果您有大数据,则应尝试在server-side尽可能多地过滤数据。

如果您没有/更改某些局部变量,您应该使用 state,否则,您应该使用redux store

我希望这可以帮助你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM