【发布时间】:2018-09-04 10:47:23
【问题描述】:
我使用了动态状态来为每个 ListItem 提供一个单独的状态,以便折叠是否打开。但是,由于我必须将参数传递给 handleClick 函数,因此我的渲染正在经历一个无限循环。
我得到的错误是
Lambdas are forbidden in JSX attributes due to their rendering performance impact
它不允许我使用 Lambda,所以我不能这样做
<ListItem button={true} onClick={() => props.handleClick(index)}>
我想知道是否有办法解决这个问题,这样我就可以让每个列表项的 handleClick 及其索引值,而不会经历无限循环
App.tsx
interface IState {
error: any,
intro: any,
threads: any[],
title: any,
}
export default class App extends React.Component<{}, IState> {
constructor (props : any) {
super (props);
this.state = {
error: "",
intro: "Welcome to RedQuick",
threads: [],
title: ""
};
this.getRedditPost = this.getRedditPost.bind(this)
this.handleClick = this.handleClick.bind(this)
}
public getRedditPost = async (e : any) => {
e.preventDefault();
const subreddit = e.target.elements.subreddit.value;
const redditAPI = await fetch('https://www.reddit.com/r/'+ subreddit +'.json');
const data = await redditAPI.json();
console.log(data);
if (data.kind) {
this.setState({
error: undefined,
intro: undefined,
threads: data.data.children,
title: data.data.children[0].data.subreddit.toUpperCase()
});
} else {
this.setState({
error: "Please enter a valid subreddit name",
intro: undefined,
threads: [],
title: undefined
});
}
}
public handleClick = (index : any) => {
this.setState({ [index]: true });
}
public render() {
return (
<div>
<Header
getRedditPost={this.getRedditPost}
/>
<p className="app__intro">{this.state.intro}</p>
{
this.state.error === "" && this.state.title.length > 0 ?
<LinearProgress />:
<ThreadList
error={this.state.error}
handleClick={this.handleClick}
threads={this.state.threads}
title={this.state.title}
/>
}
</div>
);
}
}
Threadlist.tsx
<div className="threadlist__subreddit_threadlist">
<List>
{ props.threads.map((thread : any, index : any) =>
<div key={index} className="threadlist__subreddit_thread">
<Divider />
<ListItem button={true} onClick={props.handleClick(index)}/* component="a" href={thread.data.url}*/ >
<ListItemText primary={thread.data.title} secondary={<p><b>Author: </b>{thread.data.author}</p>} />
{props[index] ? <ExpandLess /> : <ExpandMore />}
</ListItem>
<Collapse in={props[index]} timeout="auto" unmountOnExit={true}>
<p>POOP</p>
</Collapse>
<Divider />
</div>
) }
</List>
</div>
错误:
已超过最大更新深度。这可能发生在组件 在 componentWillUpdate 内重复调用 setState 或 组件更新。 React 将嵌套更新的数量限制为 防止无限循环。
【问题讨论】:
-
问题在这里:
onClick={props.handleClick(index),分配一个函数而不是值,像这样:onClick={() => props.handleClick(index)}或使用柯里化概念:public handleClick = (index : any) = () => { this.setState({ [index]: true }); } -
查看此答案以获取更多详细信息。 Maximum update depth exceeded
-
检查this问题并讨论ios。
-
如果我尝试分配一个函数,它会给我一个错误提示“由于其渲染性能影响,在 JSX 属性中禁止使用 Lambda”
标签: reactjs typescript render