【发布时间】:2022-01-26 02:54:42
【问题描述】:
我有一个叫做下载的按钮,它是一个组件。功能写在一个容器中。
我希望按钮默认隐藏,除非下载函数中的“(i === number)”条件为真。
但是,这很棘手,因为只有在我单击“下载”时才会验证此条件。我不确定如何事先验证此逻辑以确定是否需要显示按钮。
你能帮忙吗?我正在尝试设置显示按钮的状态,但它不起作用。
我的代码基本上是这样的 -
容器:
state = {
showButton: false,
};
componentDidMount() {
this.download();
};
download = async () => {
const data = await this.props.client
.query({
query: numberQuery,
fetchPolicy: "no-cache",
})
// retrive data from number query ....
const contentData = data.data.content;
// retrieve and format numbers
const numbers = this.getNumbers(contentData);
// call get number and get the individual number here
const number = await this.getNumber();
numbers.forEach((i) => {
// this is to check if numbers contain the number from getNumber(), if
// number matches number in numbers
if (i === number) {
this.setState({
showButton: true,
});
// call functions, start downloading
};
});
};
render() {
return (
{this.state.showButton ?
<Download
onStartDownload={() => this.download()}
/> : null}
);
};
组件:
class Download extends Component {
state = {
startDownload: false,
};
startDownload = () => {
this.props.onStartDownload();
};
render() {
return (
<Fragment>
<Button
id="download"
onClick={this.startDownload}
>
Download
</Button>
</Fragment>
);
};
};
【问题讨论】:
标签: javascript css reactjs components