Javascript 如何直接调用道具?

Javascript 如何直接调用道具?,javascript,reactjs,Javascript,Reactjs,我试图直接调用props函数this.props.setAuth(),但它似乎不起作用:/ TypeError:无法读取未定义的属性“props” 如何直接调用子组件中的父函数 我想在不使用任何事件处理程序的情况下更改父状态,例如onClick={this.props.setAuth}。我想在异步函数中实现它,所以是的 任何替代方案都会有帮助。谢谢:c 编辑:额外代码: class LoginPage extends Component { constructor(props){ su

我试图直接调用props函数
this.props.setAuth(),但它似乎不起作用:/

TypeError:无法读取未定义的属性“props”

如何直接调用子组件中的父函数

我想在不使用任何事件处理程序的情况下更改父状态,例如
onClick={this.props.setAuth}
。我想在异步函数中实现它,所以是的

任何替代方案都会有帮助。谢谢:c

编辑:额外代码:

class LoginPage extends Component {
  constructor(props){
   super(props)
   this.state = {auth: false}
   this.setAuth = this.setAuth.bind(this)
  }

  setAuth() {
    this.setState({auth: true})
  }
 <App setAuth={this.setAuth}/>
(无用代码)-onSignIn位于应用程序组件内

        axios.post('http://localhost:3000/auth', {
         userId: id,
         name: name,
         email: email
        })
        .then(function (response) {
        this.props.setAuth();

        })
        .catch(function (error) {
          console.log(error);
        });


         }.bind(this));
这里有一些例子:

我的索引文件具有以下组件和功能: 组件将作为道具接收回调函数

<ArtistsSearchList 
    onArtistSelect={
        current_artist =>this.setArtist(current_artist)
    }
    key={this.state.term}
    access_token=access_token}
    term={this.state.term} />
在ArtistsSearchList组件文件中,我有以下内容: 我将再次作为道具传递组件ArtistsSearchListItem的方法

 <ArtistsSearchListItem
                key={artist.id}
                current_artist={artist}
                onArtistSelect={this.props.onArtistSelect}
                artist_image_url={artist.images[0].url} />

网络应用程序正在运行:

一定有办法做到你所说的。您可以发布一些代码,让我们看看您是如何实现的吗?添加一些代码以提供更多帮助。此外,从您遇到的错误来看,似乎
未在您试图调用的
this.props
范围内定义。您是否使用函数类型的子组件(即不是类)?
。然后((响应)=>{
。catch((错误)=>{
我认为@Emersonct就在其中。this.props.setAuth将具有promise的“then”函数的范围。您可能可以在axios函数之外使用胖箭头或捕获var self=this。使用有限的代码很难准确判断,但如果您放置一个console.log(this),则底线是在使用this.props.setAuth之前,我想您会看到范围不是您所认为的范围。这是一个很好的示例,感谢您与我分享。:)如果您喜欢,请竖起大拇指:)
setArtist(current_artist){
        this.setState({
            current_artist,
            artist_image_url: current_artist.images[0].url
        });
        var spotifyApi = new SpotifyWebApi();
        spotifyApi.setAccessToken(access_token);
        spotifyApi.getArtistAlbums(current_artist.id, {album_type: 'album', limit: 10, market: 'BR'}, (err, data) => {
        if(err){
            console.log(err);
            // window.location.replace(api_url);
        }
        console.log(access_token);
        console.log("Data: ", data);
        console.log("State current artist: ", this.state.current_artist);
        this.setState({
            albums: data.items,
            selected_album_id: data.items[0].id
        });
    });
}
 <ArtistsSearchListItem
                key={artist.id}
                current_artist={artist}
                onArtistSelect={this.props.onArtistSelect}
                artist_image_url={artist.images[0].url} />
const ArtistSearchListItem = (props) => {
    const artist_name = props.current_artist.name;
    const artist_image_url = props.artist_image_url;
    return(
        <li onClick={() => props.onArtistSelect(props.current_artist)} className="artist-search-list-item">
            <h3>{artist_name}</h3>
            <img alt="Foto do artista" className="img-responsive img-thumbnail" src={artist_image_url}/>
        </li>
    );
}