Я столкнулся с проблемой, когда я не могу выполнить функцию setState внутри какой-либо функции в React. Я просматриваю решение, и мне кажется, что мне нужно использовать функцию стрелки или функцию связывания, но я все еще не понимаю, как это работает. Вот мой кусок кода:
class App extends Component {
constructor(props) {
super(props);
this.state = { teamsId: "" };
}
componentDidMount() {
var x = "";
/*
some initialization here, but not really relevant to my question
*/
//Test context
microsoftTeams.getContext(function(context)
{
//in this part, x is filled with the Id, but how to pass the value to state?
x = context.teamId;
console.log(x);
})
//in this part, x is empty again
console.log(x);
this.setState({ teamsId: x });
}
}
Как передать context.teamId в мое состояние?