1

当我在下面进行调用时,我想将一个 url 传递给this.props.saveFileInputAnswer但我得到一个错误,说它Error TypeError: _this.props.saveFileInputAnswer不是一个函数。saveFileInputAnswer在父组件中定义并在当前组件中称为道具。

Axios({
  url: CLOUDINARY_URL,
  method: "POST",
  skipAuthorization: true,
  headers: {
    "Content-Type": "application/x-www-form-urlencoded"
  },
  data: fd
})
  .then(response => {
    this.getFileUrl(response.data.secure_url);

    if (response.statusText === "OK") {
      this.setState({ isLoading: false });
      //console.log(this.state);
      toast("File Upload Successful");
      this.props.saveFileInputAnswer(
        this.state.fileUrl,
        this.props.item._id
      );
    }
  })
  .catch(error => {
    console.log("Error", error);
  });
4

1 回答 1

0

看起来您没有将 saveFileInputAnswer 作为道具传递给子组件。您需要将 saveFileInputAnswer 传递给 Child 组件。由于您尚未分享父组件的外观,请考虑以下示例以更好地理解

考虑您在组件 A 中声明了 saveFileInputAnswer。

     class AComponent extends Component{

            saveFileInputAnswer = () => {

            }

            render(){
                 return(
                     <div><BComponent saveFileInputAnswer={this.saveFileInputAnswer} /></div>
                 )
             }
     }

现在在组件 B 中,您可以访问 this.props.saveFileInputAnswer

于 2018-10-19T15:15:00.053 回答