[英]How to execute a function while the page is loading with quaternary operator
我正在学习本教程并慢慢地在我自己的方向上磨磨蹭蹭。 有一个房屋列表,带有一个链接按钮,可将您发送到包含人员列表的页面。 但是,当您通过house
单击链接按钮时,它将在houseId
中显示 houseId。 当人员列表加载时,我希望它检查 url 是否有houseId
。
如果它具有 Id,则执行getPeopleFromHouse
,它基本上axios
并将数据放入people: []
中,它位于this.state = {...}
中。 很难制定这个问题,所以这里的上下文是我拥有的代码:
getPeopleFromHouse = (HouseId) => {
axios.get("http://localhost:8080/house/getPeople/" + HouseId)
.then(response => response.data)
.then((data) => {
this.setState({people:data});
});
}
这是在render()
的return
中,我基本上想在有 Id 时摆脱按钮(添加、编辑、删除)并最后执行该方法,以便它只显示来自那个房子的人:
<Card.Body>
<table striped bordered hover variant="dark">
<thead>
<tr>
<th>Name</th>
<th>Phone Number</th>
<th>#</th>
</tr>
</thead>
<tbody onLoad={!isNaN(parseInt(this.state.urlLastId)) === true ? this.getPeopleFromHouse(this.state.urlLastId) :
null}> {this.state.people.length === 0 ?
<tr align="center">
<td colspan="{3}">{people.length} Users Available.</td>
</tr>
: currentPeople.map((person) => (
<tr key="{person.PersonId}">
<td>{person.FullName}</td>
<td>{person.PhoneNumber}</td>
<td>
<ButtonGroup>
<Link to={"addPersonToHouse/" + person.PersonId} className="btn btn-sm btn-outline-info">Add To House </Link>
<Link to={"editPerson/" + person.PersonId} className="btn btn-sm btn-outline-primary">Edit </Link>
<button size="sm" variant="outline-danger" onClick="{this.deletePerson.bind(this," person.PersonId)}>
Delete
</button>
</ButtonGroup>
</td>
</tr>
)) }
</table>
</Card.Body>
最后一件事对格式化感到抱歉,但是当我 ctrl+v 我的代码时,它只会奇怪地格式化,我不知道如何修复它。
编辑:@Always Learning 的解决方案很好,但它不起作用,因为我还没有为它制定路线,所以我添加了<Route path={"/getPeopleFromHouse/:houseId"} exact component={PersonList}/>
现在它可以工作了:D
我发现在 return 语句之前实现这样的逻辑更好,所以你可以这样做:
const getPeopleRow = () => {
console.log(">>>getPeopleRow called", this.state.people.length);
return (
<tr align="center">
<td colspan="{3}">{this.state.people.length} Users Available.</td>
</tr>
);
}
const getButtonGroup = (currentPeople) => {
console.log(">>>getButtonGroup called",currentPeople);
return currentPeople.map((person) => (
<tr key="{person.PersonId}">
<td>{person.FullName}</td>
<td>{person.PhoneNumber}</td>
<td>
<ButtonGroup>
<Link to={"addPersonToHouse/" + person.PersonId} className="btn btn-sm btn-outline-info">Add To House </Link>
<Link to={"editPerson/" + person.PersonId} className="btn btn-sm btn-outline-primary">Edit </Link>
<button size="sm" variant="outline-danger" onClick="{this.deletePerson.bind(this," person.PersonId)}>
Delete
</button>
</ButtonGroup>
</td>
</tr>
));
}
render() {
let rows;
if (this.state.people.length === 0) {
rows = this.getPeopleRow();
}
else rows = this.getButtonGroup(currentPeople);
console.log(">>>got Rows:",rows);
return (
<Card.Body>
<table striped bordered hover variant="dark">
<thead>
<tr>
<th>Name</th>
<th>Phone Number</th>
<th>#</th>
</tr>
</thead>
<tbody onLoad={!isNaN(parseInt(this.state.urlLastId)) === true ? this.getPeopleFromHouse(this.state.urlLastId) :
null}>
{ rows }
</table>
</Card.Body>
);
}
甚至可能将onLoad
处理移到 React 生命周期方法之外,以便在组件加载时启动它,而不是在表加载时启动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.