尝试这个:
const styles = {
fontFamily: "sans-serif"
};
const CSSTransitionGroup = React.addons.CSSTransitionGroup;
class App extends React.Component {
constructor() {
super();
this.state = {
viewMembers: true,
members: [
{
id: 1,
name: "Jim",
isMember: true
},
{
id: 2,
name: "John",
isMember: false
},
{
id: 3,
name: "Sarah",
isMember: false
},
{
id: 4,
name: "Mary",
isMember: true
}
]
};
this.refreshState = true;
}
changeViewState(state) {
this.refreshState = false;
setTimeout(() => {
if (state === "members") {
this.setState({ viewMembers: true });
if(!this.refreshState)
{
this.changeViewState(state);
this.refreshState = true;
}
} else {
this.setState({ viewMembers: false });
if(!this.refreshState)
{
this.changeViewState(state);
this.refreshState = true;
}
}
}, 500);
}
render() {
return (
<div style={styles}>
<h4>Members</h4>
<ul>
<li onClick={() => this.changeViewState("members")}>View Members</li>
<li onClick={() => this.changeViewState("non-members")}>
View Non-members
</li>
</ul>
<MembersTable
viewMembers={this.state.viewMembers}
members={this.state.members}
refreshState = {this.refreshState}
/>
</div>
);
}
}
const MembersTable = (props) =>
<table>
<thead>
<tr>
<td>
<strong>Id</strong>
</td>
<td>
<strong>Name</strong>
</td>
</tr>
</thead>
<CSSTransitionGroup
component="tbody"
transitionName="fade"
transitionAppear={true}
transitionAppearTimeout={500}
transitionEnterTimeout={500}
transitionLeaveTimeout={500}
>
{props.members.map(member => {
if (
(props.viewMembers && member.isMember && props.refreshState) ||
(!props.viewMembers && !member.isMember && props.refreshState)
) {
return (
<tr key={member.id}>
<td>
{member.id}
</td>
<td>
{member.name}
</td>
</tr>
);
}
})}
</CSSTransitionGroup>
</table>;
ReactDOM.render(<App />, document.getElementById("root"));
我添加了一个refreshState
变量并在setTimeout
函数中添加的回调函数中对其进行了更新changeViewState
。
这是更新的小提琴
希望这可以帮助。