admin管理员组文章数量:1023213
could you please tell me how to move one ponent to another ponent on button click in react ?
I get the react-router.js
from cdn .I don't know how to use this js ..I want to show second ponent on button click of
first ponent` here is my code
class Abc extends React.Component {
handle(){
alert('move to second ponent')
}
render (){
return (<div><h1>second</h1><button onClick={this.handle}>move to second page</button></div>);
}
}
class Pqr extends React.Component {
render (){
return (<div><h1>second</h1><button>click</button></div>)
}
}
class Sqr extends React.Component {
render (){
return <h1>third</h1>
}
}
ReactDOM.render(<Abc/>,document.getElementById('root'));
could you please tell me how to move one ponent to another ponent on button click in react ?
I get the react-router.js
from cdn .I don't know how to use this js ..I want to show second ponent on button click of
first ponent` here is my code
http://codepen.io/anon/pen/jVoZjW?editors=1010
class Abc extends React.Component {
handle(){
alert('move to second ponent')
}
render (){
return (<div><h1>second</h1><button onClick={this.handle}>move to second page</button></div>);
}
}
class Pqr extends React.Component {
render (){
return (<div><h1>second</h1><button>click</button></div>)
}
}
class Sqr extends React.Component {
render (){
return <h1>third</h1>
}
}
ReactDOM.render(<Abc/>,document.getElementById('root'));
Share
Improve this question
edited Dec 28, 2016 at 8:38
suman j
6,98012 gold badges62 silver badges117 bronze badges
asked Dec 25, 2016 at 4:24
user944513user944513
12.8k52 gold badges185 silver badges348 bronze badges
3
- When you say "move" do you mean "show"? – Matthew Herbst Commented Dec 25, 2016 at 4:35
- yes to to show second ponent using router js – user944513 Commented Dec 25, 2016 at 7:58
- Not sure you're understanding the use-case of React-Router. React-Router is meant for when you change the url, to show a different hierarchy of ponents. If you're not changing the route (url), then you shouldn't be using React-Router. – Matthew Herbst Commented Dec 27, 2016 at 1:40
1 Answer
Reset to default 2There is many ways this can be acplished, all of which have there place. This is but one of many methods:
I left an alternitive to this answer aswell this answer on the codepen: http://codepen.io/dirtyredz/pen/gLJeWY
class Abc extends React.Component {
constructor(){
super();
this.state={first: true};
}
handle(){
alert('move to second ponent')
this.setState({first: false})
}
render (){
return (
<div>
<button onClick={this.handle.bind(this)}>move to second page</button>
{this.state.first == true && <Pqr/>}
{this.state.first == false && <Sqr/>}
</div>
);
}
}
class Pqr extends React.Component {
render (){
return (<div><h1>First</h1></div>)
}
}
class Sqr extends React.Component {
render (){
return <h1>Second</h1>
}
}
ReactDOM.render(<Abc/>,document.getElementById('root'));
This is a quick example, like i said earlier others may be better but this works as expected.
could you please tell me how to move one ponent to another ponent on button click in react ?
I get the react-router.js
from cdn .I don't know how to use this js ..I want to show second ponent on button click of
first ponent` here is my code
class Abc extends React.Component {
handle(){
alert('move to second ponent')
}
render (){
return (<div><h1>second</h1><button onClick={this.handle}>move to second page</button></div>);
}
}
class Pqr extends React.Component {
render (){
return (<div><h1>second</h1><button>click</button></div>)
}
}
class Sqr extends React.Component {
render (){
return <h1>third</h1>
}
}
ReactDOM.render(<Abc/>,document.getElementById('root'));
could you please tell me how to move one ponent to another ponent on button click in react ?
I get the react-router.js
from cdn .I don't know how to use this js ..I want to show second ponent on button click of
first ponent` here is my code
http://codepen.io/anon/pen/jVoZjW?editors=1010
class Abc extends React.Component {
handle(){
alert('move to second ponent')
}
render (){
return (<div><h1>second</h1><button onClick={this.handle}>move to second page</button></div>);
}
}
class Pqr extends React.Component {
render (){
return (<div><h1>second</h1><button>click</button></div>)
}
}
class Sqr extends React.Component {
render (){
return <h1>third</h1>
}
}
ReactDOM.render(<Abc/>,document.getElementById('root'));
Share
Improve this question
edited Dec 28, 2016 at 8:38
suman j
6,98012 gold badges62 silver badges117 bronze badges
asked Dec 25, 2016 at 4:24
user944513user944513
12.8k52 gold badges185 silver badges348 bronze badges
3
- When you say "move" do you mean "show"? – Matthew Herbst Commented Dec 25, 2016 at 4:35
- yes to to show second ponent using router js – user944513 Commented Dec 25, 2016 at 7:58
- Not sure you're understanding the use-case of React-Router. React-Router is meant for when you change the url, to show a different hierarchy of ponents. If you're not changing the route (url), then you shouldn't be using React-Router. – Matthew Herbst Commented Dec 27, 2016 at 1:40
1 Answer
Reset to default 2There is many ways this can be acplished, all of which have there place. This is but one of many methods:
I left an alternitive to this answer aswell this answer on the codepen: http://codepen.io/dirtyredz/pen/gLJeWY
class Abc extends React.Component {
constructor(){
super();
this.state={first: true};
}
handle(){
alert('move to second ponent')
this.setState({first: false})
}
render (){
return (
<div>
<button onClick={this.handle.bind(this)}>move to second page</button>
{this.state.first == true && <Pqr/>}
{this.state.first == false && <Sqr/>}
</div>
);
}
}
class Pqr extends React.Component {
render (){
return (<div><h1>First</h1></div>)
}
}
class Sqr extends React.Component {
render (){
return <h1>Second</h1>
}
}
ReactDOM.render(<Abc/>,document.getElementById('root'));
This is a quick example, like i said earlier others may be better but this works as expected.
本文标签: javascripthow to move one component to another component on button click in reactStack Overflow
版权声明:本文标题:javascript - how to move one component to another component on button click in react? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745534048a2154880.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论