简体   繁体   English

如何使用 react 和 redux 制作用户表单?

[英]How to make user form with react and redux?

when my application first loads, it fetches the user data from the server and renders the user information in a AppBar (material-ui):当我的应用程序首次加载时,它从服务器获取用户数据并在 AppBar (material-ui) 中呈现用户信息:

displayProfileLink () {
    const {user} = this.props;
    if (!user.name) {
      return <CircularProgress style={{color: 'white'}}/>
    } else {
      return <Button component={Link} to="/profile">
        {user.name}
      </Button>
    }
  }

As you can see, there is a link to a profile component.如您所见,有一个指向配置文件组件的链接。 The user profile component is basically a form to update user data.用户配置文件组件基本上是一个用于更新用户数据的表单。 The issue is that when you first open the application using the /profile on the url, the component renders before the user data is fetched from the server, consequently the form inputs are empty and you need to navigate to other link and come back to see the user data on the inputs.问题是,当您第一次使用 url 上的 /profile 打开应用程序时,组件在从服务器获取用户数据之前呈现,因此表单输入为空,您需要导航到其他链接并返回查看输入的用户数据。

How can I update the value of the inputs when the user fetch is done without needing to switch between other links?当用户提取完成时,如何更新输入的值而无需在其他链接之间切换? If I use the component state to control the input values, the state is set when the component mounts but there is no user data fetched yet.如果我使用组件状态来控制输入值,则在组件安装但尚未获取用户数据时设置状态。

The picture bellow shows the problem:下图显示了问题: 在此处输入图片说明

Sorry about the English, thank you very much.对不起英语,非常感谢。 @---edit @---编辑

As you can see I can put the user name "Rodrigo" at the AppBar, but when the user form component loads if I use the component state to control the input values setting the component state with the user state from redux they will be empty because the user info is not fetched yet.如您所见,我可以将用户名“Rodrigo”放在 AppBar 上,但是当用户表单组件加载时,如果我使用组件状态来控制输入值,则使用 redux 中的用户状态设置组件状态,它们将为空,因为尚未获取用户信息。 The appbar displays the name because it render again after the user data is fetched but I'm not able to do the same thing with the form. appbar 显示名称,因为它在获取用户数据后再次呈现,但我无法对表单执行相同的操作。

User.jsx用户.jsx

componentDidMount() {
    const {user} = this.props;
    if (user.name) {
      this.setState({
        monetizze_x_consumer_key: user.monetizze_x_consumer_key,
        eduzz_api_key: user.eduzz_api_key,
        eduzz_public_key: user.eduzz_public_key
      })
    }
  }
<TextField          
  helperText="monetizze_x_consumer_key"
  label="Chave para geração de Token Monetizze"
  value={this.state.monetizze_x_consumer_key}
  fullWidth={true}
  required={true}
  id="monetizze_x_consumer_key"
  onChange={(event) => this.setState({monetizze_x_consumer_key: event.target.value})}
/>

When componentDidMount runs, the user data is not ready yet, so the component state remains with empty values, how could I set the state again after the user data is fetched?当componentDidMount运行时,用户数据还没有准备好,所以组件状态保持为空值,如何在获取用户数据后再次设置状态?

I think you should change state for that purpose. 我认为您应该为此目的更改状态。 I suggest reading this article: https://blog.hellojs.org/fetching-api-data-with-react-js-460fe8bbf8f2 我建议阅读这篇文章: https : //blog.hellojs.org/fetching-api-data-with-react-js-460fe8bbf8f2

You can do it with componentWillMount() Life Cycle.您可以使用 componentWillMount() 生命周期来实现。

First of all define state with empty array if data you fetch is in array or empty string if you fetch data is in string.首先,如果您获取的数据在数组中,则使用空数组定义状态,如果获取的数据在字符串中,则定义为空字符串。

then define componentWillMount() and hit api in this life cycle and set state when you receive response and after that use your state as you want.然后定义 componentWillMount() 并在此生命周期中点击 api 并在您收到响应时设置状态,然后根据需要使用您的状态。

解决者:

componentWillReceiveProps(nextProps)

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

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