当前位置: 首页 > 知识库问答 >
问题:

无法使用defaultValue设置React状态

江向阳
2023-03-14

是否有方法将defaultValue传递给状态?我也尝试了value={},但值根本不变。

class AccountEditor extends Component {
  constructor() {
    super()
    this.state = {
      isEditing: false,
      profile: {
        firstName: '',
        lastName: '',
        city: '',
        email: '',
        bio: '',
      }
    }
  }

  toggleEdit(event) {
    event.preventDefault()
    this.setState({
      isEditing: !this.state.isEditing
    })
  }

  updateProfile(event) {
    let updatedProfile = Object.assign({}, this.state.profile)
    updatedProfile[event.target.id] = event.target.value

    this.setState({
      profile: updatedProfile
    }
  }

  submitUpdate(event) {
    event.preventDefault()
    this.props.onUpdate(this.state.profile)
    this.setState({
      isEditing: !this.state.isEditing
    })
  }

  render() {
    let profile = this.props.profile
    let content = null

    if (this.state.isEditing == true) {
      content = (
        <div>
          <input
            id="firstName"
            onChange={this.updateProfile.bind(this)}
            defaultValue={profile.firstName} />
          <br />
          <input
            id="lastName"
            onChange={this.updateProfile.bind(this)}
            defaultValue={profile.lastName} />
          <br />
          <input
            id="city"
            onChange={this.updateProfile.bind(this)}
            defaultValue={profile.city} />
          <br />
          <input
            id="email"
            onChange={this.updateProfile.bind(this)}
            defaultValue={profile.email} />
          <br />
          <textarea
            id="bio"
            onChange={this.updateProfile.bind(this)}
            defaultValue={profile.bio} />
          <br />
          <button onClick={this.submitUpdate.bind(this)}>Done</button>
        </div>
      )
    } else {
      content = (
        <div>
          <h4>Name: </h4>
          <span>{profile.firstName}</span>
          <span>{profile.lastName}</span><br/>
          <h4>City: </h4>
          <span>{profile.city}</span><br/>
          <h4>Bio :</h4>
          <p>{profile.bio}</p><br />
          <button onClick={this.toggleEdit.bind(this)}>Edit</button>
        </div>
      )
    }

    return (
      <div>
        {content}
      </div>
    )
  }
}

export default AccountEditor

共有1个答案

楚翰
2023-03-14

您应该将DefaultValue替换为value={this.state.someprop}。因此,您的代码示例如下

constructor(props) {
    super(props)
    this.state = {
      isEditing: false,
      profile: props.profile // Setting up the initial data from the passed prop
    }
  }

<input id="firstName"
       onChange={ this.updateProfile.bind(this) }
       value={ this.state.profile.firstName } />

有关在这些文档中使用react with form元素的更多信息。

 类似资料:
  • 我已经创建了反应应用程序与顶点以下https://how-to.vertx.io/single-page-react-vertx-howto/.我已经设置反应路由使用反应路由器工作正常当我使用内部反应节点服务器运行在localhost:3000default.But当我trid通过vertx静态处理程序提供静态页面除了默认/路由其他路由返回“未找到”输入图像描述在这里

  • 问题内容: 我需要设置一个从事件中获取的状态字段,但是当我将一个函数传递给它时,它不会被设置。组件和方法如下所示: 如果删除,则会出现以下错误: 由于性能原因,此综合事件被重用。如果看到此消息,则说明正在释放/无效化的合成事件上访问该方法。这是一项无操作功能。如果必须保留原始的合成事件,请使用event.persist()。有关更多信息,请参见https://facebook.github.io/

  • 提前谢了。我有一个状态数组,如下所示。 我需要添加一个项目到状态数组,我发现我们不需要做状态突变。如何使用PrevState设置状态。 如何调用set State以追加此状态数组。 像这样的东西?

  • 我想用React useState钩子设置多个状态值,这样我就可以用useEffect钩子分别更新它们。我有以下代码: 但是当我用React DevTools检查应用程序时,我会看到钩子的多个“状态”值,而不是“订单”、“支付”、“提交”等。

  • 问题内容: 我正在创建一个简单的聊天应用程序,在其中我通过axios对数据库进行api调用,该API返回了一系列消息对象。我可以在componentWillMount中进行axios调用时获取数据。然后,我试图设置状态以显示对话。这是代码: 我看过一些有关生命周期功能和设置状态的帖子,似乎我在做正确的事情。 再次强调,axios调用正常,设置状态不正常。我仍然看到一个空数组。提前致谢! 编辑:这是

  • 从输入字段中,我将值作为参数发送给设置状态的函数。我有多个输入字段,所以希望使用它们的名称(等于它们的状态键),然后使用相同的函数,并将键和值传递给设置状态的函数。 这是我的代码。