【问题标题】:axios cannot resolve state element reactaxios 无法解析状态元素反应
【发布时间】:2017-09-27 14:09:32
【问题描述】:

我对反应和遵循教程还很陌生。讲师正在使用 axios 从 github api 中提取一些数据。以下是他建议的 onSubmit 事件处理程序:

handleSubmit = (event) => {
    event.preventDefault();
  console.log('event: form submit ' + this.state.userName )
  axios.get('https://api.github.com/users/${this.state.userName}')
    .then(resp => {
        console.log(resp)
    });

}

但是,当我运行它时,this.state.userName 没有得到解决,我收到 404。代码有问题还是 axios 更新了?我正在使用 jscomplete/repl 游乐场来使用它。

帮助表示赞赏!

以下是组件代码:

class Form extends React.Component {

    state = {
userName: '  ',
event: ' '
}

handleSubmit = (event) => {
    event.preventDefault();
  console.log('event: form submit ' + this.state.userName )
  axios.get('https://api.github.com/users/${this.state.userName}')
    .then(resp => {
        this.setState({event: resp})
    });
  console.log(event)
}

render() {
  return (
    <form onSubmit={this.handleSubmit}>
      <input type="text"
                    value={this.state.userName}
        onChange={(event) => this.setState({userName: event.target.value})}
                    placeholder={ this.state.username } required 
      />
      <button>Add Card</button>
    </form>
  )
}
}

【问题讨论】:

  • 仅供参考,我得到了这个:GET api.github.com/users/$%7Bthis.state.userName%7D 404(未找到)
  • 你能说明你是怎么称呼它的吗?可能是完整的组件代码
  • 好的,我会编辑问题
  • 那么 this.state.userName 肯定是未定义的!在你旁边缺少 `(反引号)

标签: javascript reactjs ecmascript-6 axios


【解决方案1】:

您没有像应有的那样使用ES6 Template literals
在这一行中,您使用 ':
'https://api.github.com/users/${this.state.userName}' 包装了字符串

用反引号 (`) 包装它(使用波浪号按钮)

【讨论】:

  • 呃...这是我的一个非常蹩脚的错误@Sag1v...帮助感谢芽!
  • 有趣的是,我在解析的 url 中得到了垃圾:api.github.com/users/%20%20ovk23.. 无法理解这个 %20%20!这是因为“this”用错了吗?
  • 它的 url 编码值 %20space,好像你的值中有双倍空格。尝试修剪这个vlue,你会得到api.github.com/users/ovk23
【解决方案2】:

您确实应该使用构造函数来初始化状态变量。使用反引号(`) 替换字符串中的变量。还将函数绑定到类以使用this 上下文。试试下面的代码

class Form extends React.Component {

    constructor(props) {
        super(props)
        this.state = {
            userName: '  ',
            event: ' '
        }

        this.handleSubmit = this.handleSubmit.bind(this)
    }

    handleSubmit = (event) => {
        event.preventDefault();
        console.log('event: form submit ' + this.state.userName)
        axios.get(`https://api.github.com/users/${this.state.userName}`)
            .then(resp => {
                this.setState({ event: resp })
            });
        console.log(event)
    }

    render() {
        return (
            <form onSubmit={this.handleSubmit}>
                <input type="text"
                    value={this.state.userName}
                    onChange={(event) => this.setState({ userName: event.target.value })}
                    placeholder={this.state.username} required
                />
                <button>Add Card</button>
            </form>
        )
    }
}

【讨论】:

  • 我曾假设,在 ES6 语法中,您可以直接使用状态变量而不是声明构造函数......不过我肯定会尝试这个。
  • 可以,但bind 函数在constructorcomponentDidMount 中有实际位置。如果它们很少,我更喜欢在构造函数中绑定它们
  • 有趣的是,我在解析的 url 中得到了垃圾:api.github.com/users/%20%20ovk23.. 无法理解这个 %20%20!这是因为“this”用错了吗?
  • %20%20 是空格。因为你有this.state.username = ' '。注意空间。尝试输入一些有效值
  • 最后...感谢 Priyesh Kumar!它最终确实奏效了...... 1/2 小时的头痛消失了...... :)
猜你喜欢
  • 1970-01-01
  • 2015-03-31
  • 1970-01-01
  • 2017-08-24
  • 1970-01-01
  • 1970-01-01
  • 2018-05-18
  • 2021-10-23
  • 1970-01-01
相关资源
最近更新 更多