[英]How to change component inside of render with a button click?
I'm new at programming at all especially in ReactJS.我是编程新手,尤其是 ReactJS。 Actually I'm running into a problem.
实际上我遇到了问题。
I just wanna change the page.我只是想换个页面。 As simple as it sounds for me it is a 2 day Google-Search-Nothing-Found-Adventure without fun.
对我来说听起来很简单,这是一个没有乐趣的 2 天 Google-Search-Nothing-Found-Adventure。
I made a component called <Frontpage />
It renders directly in the beginning but I want to change it with a click on a button.我制作了一个名为
<Frontpage />
的组件,它在开始时直接呈现,但我想通过单击按钮来更改它。
This is my code:这是我的代码:
import React from 'react';
import './App.css';
import Frontpage from './Frontpage'
import Question from './components/Question'
class App extends React.Component {
constructor() {
super()
this.state = {
showComponent: true,
}
}
handleClick() {
if (this.state.showComponent) {
return <Question />
} else {
console.log("Something went wrong!")
}
// console.log("The button was clicked!")
// document.getElementById('page')
// document.getElementsByClassName('App-Header')
}
render() {
return (
<div className="App">
<header className="App-header">
<div id="page">
<Frontpage />
</div>
<button id="button" onClick={this.handleClick}>Los geht's</button>
</header>
</div>
);
}
}
export default App;
When I click it, it always says: "TypeError: Cannot read property 'state' of undefined"当我单击它时,它总是说:“TypeError:无法读取未定义的属性'状态'”
I tried a lot but nothing worked.我尝试了很多,但没有任何效果。
Try something like this.尝试这样的事情。 Change you
handleClick
function to an arrow function and render the component based on the state.把你的
handleClick
function改成箭头function,然后根据state渲染组件。
Update : notice that i have used a Fragment
to wrap the button
and Question
elements/components更新:请注意,我使用了
Fragment
来包装button
和Question
元素/组件
import React,{Fragment} from 'react';
import './App.css';
import Frontpage from './Frontpage'
import Question from './components/Question'
class App extends React.Component {
constructor() {
super()
this.state = {
showComponent: false,
}
}
handleClick =()=> {
this.setState({showComponent:true})
}
render() {
return (
<div className="App">
<header className="App-header">
<div id="page">
{this.state.showComponent? <Fragment><Question /><button id="button" onClick={this.handleClick}>Los geht's</button></Fragment> : <Frontpage /> }
</div>
</header>
</div>
);
}
}
export default App;
You have to bind the handleClick
to your class component
or in other case, use an arrow function: To bind the method at the bottom of your state
inside the constructor
add this:您必须将
handleClick
绑定到您的class component
,或者在其他情况下,使用箭头 function:要在state
底部绑定方法,请在constructor
中添加:
this.handleClick = this.handleClick.bind(this);
To use the arrow function change the syntax of your method as:要使用箭头 function将方法的语法更改为:
this.handleClick = () => {
if (this.state.showComponent) {
return <Question />
} else {
console.log("Something went wrong!")
}
}
import React from 'react';
import './App.css';
import Frontpage from './Frontpage'
import Question from './components/Question'
class App extends React.Component {
constructor() {
super()
// Start with a hide component
this.state = {
showComponent: false,
}
// Binding your function
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
const { showComponent } = this.state;
// Use state to trigger a re-render
this.setState({
showComponent: !showComponent;
})
}
render() {
const { showComponent } = this.state;
return (
<div className="App">
<header className="App-header">
<div id="page">
{
// Conditionally render a component
showComponent
? (<Question />)
: (<Frontpage />)
}
</div>
<button id="button" onClick={this.handleClick}>Los get's</button>
</header>
</div>
);
}
}
export default App;
you have to bind the context to your handler https://reactjs.org/docs/handling-events.html您必须将上下文绑定到您的处理程序https://reactjs.org/docs/handling-events.html
Try this one.试试这个。
import Question from './components/Question'
class App extends React.Component {
constructor() {
super()
this.state = {
showComponent: true,
}
}
handleClick() {
this.setState({ showComponent: true });
}
render() {
const { showComponent } = this.state;
return (
<div className="App">
<header className="App-header">
<div id="page">
<Frontpage />
</div>
{ showComponent ? <Question /> : null }
<button id="button" onClick={this.handleClick.bind(this)}>Los geht's</button>
</header>
</div>
);
}
}
export default App;
Firstly, you need to bind your handle click or use arrow function like handleClick = () => {}
首先,您需要绑定您的手柄点击或使用箭头 function 像
handleClick = () => {}
Secondly, For navigation use react-router其次,导航使用 react-router
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.