在使用 BIND() 将自定义组件方法绑定到组件时,我们可以将第一个参数作为组件本身传递吗?

While binding a custom component method to the component using BIND(), Can we pass in the first argument as the component itself?

我正在尝试使用 "bind" 将自定义组件方法绑定到组件,因为在自定义组件方法内部 "this" 实际上是空的。如果这听起来很愚蠢,我深表歉意,但我们不能将第一个参数作为组件本身传递给 bind 方法,而不是将第一个参数作为 "this" 传递吗?因为这无论如何指的是组件本身。

        function FriendsList(props){
            return(
            <ul>
                {props.list.map((name)=>(
                    <li key={name}>
                        <span>{name}</span>
                        <button onClick= 
                         {()=>props.onRemoveFriend(name)}>Remove</button>
                    </li>
                    ))}
            </ul> 
            )


        }
        class App extends React.Component{
            constructor(props){
                super(props)

                this.state={
                    friends:['Batman','Thor','Hulk']
                }
                **this.handleRemoveFriend=this.handleRemoveFriend.bind(this)**
            }
            handleAddFriend(){

            }
            handleRemoveFriend(name){
                  this.setState((currentState)=>{
                    return{

                friends:currentState.friends.filter((friend)=>friend!==name)
                    }
                  })
            }
            render(){
                return(
                    <div>
                        <FriendsList 
                        list={this.state.friends}
                        onRemoveFriend={this.handleRemoveFriend}
                        />
                    </div>
                )
            }

        }

        ReactDOM.render(<App />, document.getElementById('app'))


    </script>```

instead of "this.handleRemoveFriend=this.handleRemoveFriend.bind(this)". Can't we write "this.handleRemoveFriend=this.handleRemoveFriend.bind(App)"?
It gives me an error when I do so, saying this.setstate is not a function.

你不能。 this 是您的 class 的实例,App 是 class 本身。