reactjs - 从组件反应中的另一个类获取对象
问题描述
早上好,我是新手,我正在努力解决问题。我找到了一个 spotify 按钮,它从您的cliendID
. 看起来像这样
import React from 'react';
import ReactDOM from 'react-dom';
import SpotifyLogin from 'react-spotify-login';
import { clientId, redirectUri } from './settings';
const onSuccess = response => console.log(response);
const onFailure = response => console.error(response);
ReactDOM.render(
<SpotifyLogin clientId={clientId}
redirectUri={redirectUri}
onSuccess={onSuccess}
onFailure={onFailure}/>,
document.getElementById('example')
);
我想得到的数据是response
来自它,因为它有access_token
. 作为新的反应,我想我可以制作一个带有吸气剂的课程。
import React, { Component, setState, useState } from 'react';
import SpotifyLogin from 'react-spotify-login';
import { Link } from 'react-router-dom'
class App extends Component {
constructor() {
super()
this.state = {
onSuccess : {}
}
}
getData() {
return this.onSuccess;
}
render(){
return (
<div>
<Link to="/home">
<SpotifyLogin buttonText="Login" clientId='myinfo'
redirectUri='http://localhost:3000/home'
onSuccess={this.onSuccess}/>
</Link>
</div>
)
}
}
export default App
但在我的另一个组件中,当我尝试做一个
var toto = new App
console.log(toto.getData);
都是空的。此外,我想知道是否有一种方法可以让我避免在每次需要访问我的数据时在我的组件中声明一个新对象。例如,我将传递props
给我的类,所以如果我在声明它的实例的文件中调用这个组件App
就可以了,但是如果我在另一个文件中使用这个组件并且没有实例App
,我怎么会有访问这些数据。有办法global
吗?
解决方案
在我看来,您的类组件将this.onSuccess
返回空,因为它从未被设置。由于您已更改this.onSuccess
为状态变量,因此您需要在某个时候设置该状态。目前,您onSuccess
在渲染中的函数将不起作用,因为它根本不是函数。
因此,您需要在getData
开始工作之前更改响应状态,例如:
import React, { Component, setState, useState } from 'react';
import SpotifyLogin from 'react-spotify-login';
import { Link } from 'react-router-dom'
class App extends Component {
constructor() {
super()
this.state = {
responseObject : {}
}
}
onSuccess(response) {
this.setState({ responseObject: response})
}
getData() {
return this.responseObject;
}
render(){
return (
<div>
<Link to="/home">
<SpotifyLogin buttonText="Login" clientId='myinfo'
redirectUri='http://localhost:3000/home'
onSuccess={this.onSuccess}/>
</Link>
</div>
)
}
}
export default App
虽然,关于你的第二个问题,在更高的类中定义这个函数会更有意义onSuccess
,并像你建议的那样通过道具传递这个函数,这样你就不必使用这个getData
函数了。因此,您可以这样做:
class SpotifyContainer extends Component {
render(){
return (
<div>
<Link to="/home">
<SpotifyLogin buttonText="Login" clientId='myinfo'
redirectUri='http://localhost:3000/home'
onSuccess={this.props.onSuccess}/>
</Link>
</div>
)
}
}
因此,您可以通过onSuccess
props 将其作为函数传递。
就个人而言,我也建议使用 React Hooks / Functional 组件,因为我发现它们比类组件更容易理解,尤其是在您不打算使用状态的情况下。所以你可以改为写:
function SpotifyContainer(props) {
return (
<div>
<Link to="/home">
<SpotifyLogin buttonText="Login" clientId='myinfo'
redirectUri='http://localhost:3000/home'
onSuccess={props.onSuccess}/>
</Link>
</div>
)
}
export default SpotifyContainer;
推荐阅读
- python - WxPython 自定义弹出窗口
- mysql - 将时间间隔分成1小时间隔mysql
- bash - 以“-e”开头的 Bash 变量不存储
- vba - 按文件类型保存附件+显示随机消息
- elasticsearch - 如何将映射类型从“_doc”更改为“doc”?
- python - Pytorch 中是否有类似于 OpenCV 中的 cv2.dilate 的“张量”操作或函数?
- android - 如何在一个片段和另一个片段之间共享捕获的位图图像(通过相机)传递?
- xml - Wildfly LDAP 身份验证、属性授权上的 Drools
- java - 视图被推到水平约束布局之外
- visual-studio - Visual Studio,每个开发人员的 launchSettings.json