反应渲染功能不更新
React render function not updating
我有以下代码 - 我想更改在单击按钮时呈现的反应组件。但是单击按钮什么也不做。这是父 class:
export default class SupportsContent extends React.Component {
currentPage = "userSupportsList";
goToPage = page => {
this.currentPage = page;
}
render() {
let content = "";
if (this.currentPage === "userSupportsList") {
content = <UserSupportsList goToPage={this.goToPage} />
} else if (this.currentPage === "chooseNewSupport") {
content = <ChooseNewSupport goToPage={this.goToPage} />
} else if (this.currentPage === "editSupport") {
content = <EditSupport goToPage={this.goToPage} />
}
return (
<Grid>
{content}
</Grid>
);
}
}
我有以下子组件定义:
function UserSupportsList(props) {
return (
<ListItem button onClick={function () {props.goToPage("chooseNewSupport");}}></ListItem>
);
}
function ChooseNewSupport(props) {
return (
<p>Choose New Support</p>
);
}
function EditSupport(props) {
return (
<p>Edit Support</p>
);
}
React 根据两个条件更新组件:如果组件 props 更改或使用 this.setState
函数更新状态。
我建议您编辑组件以使用状态而不是变量:
class SupportsContent extends Component {
state = {
currentPage: "userSupportsList"
}
goToPage = page => this.setState({ currentPage: page })
render() {
let content = "";
if (this.state.currentPage === "userSupportsList") {
content = <UserSupportsList goToPage={this.goToPage} />
} else if (this.state.currentPage === "chooseNewSupport") {
content = <ChooseNewSupport goToPage={this.goToPage} />
} else if (this.state.currentPage === "editSupport") {
content = <EditSupport goToPage={this.goToPage} />
}
return (
<Grid>
{content}
</Grid>
);
}
}
我有以下代码 - 我想更改在单击按钮时呈现的反应组件。但是单击按钮什么也不做。这是父 class:
export default class SupportsContent extends React.Component {
currentPage = "userSupportsList";
goToPage = page => {
this.currentPage = page;
}
render() {
let content = "";
if (this.currentPage === "userSupportsList") {
content = <UserSupportsList goToPage={this.goToPage} />
} else if (this.currentPage === "chooseNewSupport") {
content = <ChooseNewSupport goToPage={this.goToPage} />
} else if (this.currentPage === "editSupport") {
content = <EditSupport goToPage={this.goToPage} />
}
return (
<Grid>
{content}
</Grid>
);
}
}
我有以下子组件定义:
function UserSupportsList(props) {
return (
<ListItem button onClick={function () {props.goToPage("chooseNewSupport");}}></ListItem>
);
}
function ChooseNewSupport(props) {
return (
<p>Choose New Support</p>
);
}
function EditSupport(props) {
return (
<p>Edit Support</p>
);
}
React 根据两个条件更新组件:如果组件 props 更改或使用 this.setState
函数更新状态。
我建议您编辑组件以使用状态而不是变量:
class SupportsContent extends Component {
state = {
currentPage: "userSupportsList"
}
goToPage = page => this.setState({ currentPage: page })
render() {
let content = "";
if (this.state.currentPage === "userSupportsList") {
content = <UserSupportsList goToPage={this.goToPage} />
} else if (this.state.currentPage === "chooseNewSupport") {
content = <ChooseNewSupport goToPage={this.goToPage} />
} else if (this.state.currentPage === "editSupport") {
content = <EditSupport goToPage={this.goToPage} />
}
return (
<Grid>
{content}
</Grid>
);
}
}