【问题标题】:React defaultValue not working axios deliverd dynamic dataReact defaultValue 不工作 axios 提供动态数据
【发布时间】:2017-08-17 17:11:12
【问题描述】:

你好,我是 React 的新手,我想玩一点 React,但有一点我不明白。

首先,使用返回我的数据的 axios 数据获取以下内容,然后我尝试将它们放入输入字段中,value(并且是只读的),defaultValue 更好,现在我遇到了问题,我什么也没看到,当我用萤火虫查看时该值存在,奇怪的是,当我添加一个不需要的字符时,输入会被我想要的而不是默认填充。

非常奇怪的是,当我将所有内容放入一个数组并对其执行映射函数时,我得到了值

json 代码

 {"firma":"hallo","strasse":"musterweg 7","plz":"01662"}

js 代码

import React from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';

class Testx extends React.Component {

  constructor(props) {
    super(props);

    this.state = {
      data:[]
    };
  }

  componentDidMount(){
    var self = this;

    axios.get('http://localhost/index.php')
      .then(function (response) {
        self.setState({ data: response.data});
      })
      .catch(function (error) {
        console.log(error);
      });
  }

  render() {
    return (
      <div>
        <input type="text" defaultValue={this.state.data.firma}/>
      </div>
    );
  }
}

ReactDOM.render(<Testx/>, document.getElementById('hello'));

【问题讨论】:

  • 您尝试访问数组中的属性firma 而不是对象
  • defaultValue 仅适用于初始渲染。请改用value
  • 啊,我明白了,我在这个职位上所做的一切没有任何意义,感谢您²,我想我现在可以为我创建一个适合我计划的解决方案
  • 请不要做var self = this之类的事情。那已经过时了。尝试通过使用箭头函数或在构造函数中绑定你的函数来保持this 的范围。
  • @Nocebo,嗯,好吧,我自己只是一个解决方案,我无法在我的函数中加入这个,并在网络上快速找到了这个解决方案并且运行良好

标签: javascript node.js reactjs axios


【解决方案1】:

您需要通过显示正在加载的内容等到数据到来。

import React from 'react';
import ReactDOM from 'react-dom';
 import axios from 'axios';
class Testx extends React.Component {

    constructor(props) {
  super(props);

  this.state = {
    data:{}
  };
}
componentDidMount(){
    var self = this;
        axios.get('http://localhost/index.php')
  .then(function (response) {
    self.setState({ data: response.data});
  })
  .catch(function (error) {
    console.log(error);
  });

}
  render() {
    const { data }= this.state;
    if(data.firma) {
    return (<div>
              <input type="text" defaultValue={data.firma}/>
       </div>);
    }
    return <div>loading...</div>;
  }
}

ReactDOM.render(<Testx/>, document.getElementById('hello'));

【讨论】:

  • 您可以投票并勾选答案以帮助我。
  • 您将无法访问firma:P
  • @DineshKatwal 你去 :)
  • @stefanBd 请勾选答案。
  • @DineshKatwal 我做到了,它算在内,但我的力量较小,这里没有显示,抱歉:(
【解决方案2】:

最初,您的数据状态是数组格式。所以this.state.data.firma 不起作用。而是将其设为空对象{}

import React from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';
class Testx extends React.Component {

constructor(props) {
  super(props);

  this.state = {
    data: {}
  };
}

componentDidMount() {
    var self = this;
        axios.get('http://localhost/index.php')
  .then(function (response) {
    self.setState({ data: response.data});
  })
  .catch(function (error) {
    console.log(error);
  });
}

render() {
    return <div>
              <input type="text" defaultValue={this.state.data.firma}/>
       </div>
  }
}

ReactDOM.render(<Testx/>, document.getElementById('hello'));

【讨论】:

    【解决方案3】:

    “代码风格”已过时。尝试使用绑定您的函数的箭头函数,例如setState。或者像this.myFunction = myFunction.bind(this)这样在你的构造函数中绑定你的函数一次,这样你就可以访问this。我已经评论说this.state.data 被声明为一个数组。要么将其更改为对象,要么通过特定索引访问对象。

    class Testx extends React.Component {
    
      constructor(props) {
        super(props);
    
        this.state = {
          data:{}
        };
      }
    
      componentDidMount = () => { //Note the arrow function to bind this function
        //Functions like componentDidMount are usually already bound
    
        axios.get('http://localhost/index.php')
          .then((response) => {
            this.setState({ data: response.data});
          })
          .catch((error) => {
            console.log(error);
          });
      }
    
      render() {
        return (
          <div>
            <input type="text" defaultValue={this.state.data.firma}/>
          </div>
        );
      }
    }
    

    如果您的响应是一个数组而不是一个对象,请尝试像这样访问firmathis.state.data[index].firma

    【讨论】:

      【解决方案4】:

      谢谢大家,特别是提示和技巧以及我如何才能做得更好,我的问题得到了解决,非常感谢大家在 15 分钟内帮助我很高兴

      我现在也找到了一种使用https://facebook.github.io/react/docs/forms.html 的方法并设置我的状态

       handleChange(event) {
      
              var tmp = this.state.data;
              tmp[event.target.id] = event.target.value
              this.setState({data: tmp});
        }
      

      修改我的渲染

         <input type="text" id="firma" value={this.state.data.firma} onChange={this.handleChange} />
      

      【讨论】:

      • 仅供参考:setState 是异步的。所以如果你有一天想知道为什么console.log() 在 setState 显示旧结果之后,你应该使用setState 的回调函数:this.setState({data: tmp}, () =&gt; console.log(this.state.data));
      • @stefan 请发布另一个问题
      • 您对此还有其他问题吗?
      • @Nocebo 哈哈,是的,我来自 php,所以这个异步对我来说非常特别,目前我没有任何其他问题,我今天完成(当你把你的工作带回家时很糟糕)跨度>
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 2022-12-06
      • 2020-08-24
      • 2015-07-20
      • 2021-08-29
      • 1970-01-01
      相关资源
      最近更新 更多