How to modify an existing div element in html by react?












0














Is it possible to use react to show and hide an existing div element by id?



For example, I want use react to have this <div id="example">this is example</div> show and hide by button click and this element is not created by react.










share|improve this question
























  • is this what you're looking for: stackoverflow.com/questions/24502898/….
    – Luis Osta
    Nov 13 at 17:59










  • thank you for your information, but this is not the answer, this is not about modifying the existing div.
    – fong lee
    Nov 15 at 14:44
















0














Is it possible to use react to show and hide an existing div element by id?



For example, I want use react to have this <div id="example">this is example</div> show and hide by button click and this element is not created by react.










share|improve this question
























  • is this what you're looking for: stackoverflow.com/questions/24502898/….
    – Luis Osta
    Nov 13 at 17:59










  • thank you for your information, but this is not the answer, this is not about modifying the existing div.
    – fong lee
    Nov 15 at 14:44














0












0








0







Is it possible to use react to show and hide an existing div element by id?



For example, I want use react to have this <div id="example">this is example</div> show and hide by button click and this element is not created by react.










share|improve this question















Is it possible to use react to show and hide an existing div element by id?



For example, I want use react to have this <div id="example">this is example</div> show and hide by button click and this element is not created by react.







reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 17:41









Jacob Tomlinson

1,59021946




1,59021946










asked Nov 13 at 17:00









fong lee

295




295












  • is this what you're looking for: stackoverflow.com/questions/24502898/….
    – Luis Osta
    Nov 13 at 17:59










  • thank you for your information, but this is not the answer, this is not about modifying the existing div.
    – fong lee
    Nov 15 at 14:44


















  • is this what you're looking for: stackoverflow.com/questions/24502898/….
    – Luis Osta
    Nov 13 at 17:59










  • thank you for your information, but this is not the answer, this is not about modifying the existing div.
    – fong lee
    Nov 15 at 14:44
















is this what you're looking for: stackoverflow.com/questions/24502898/….
– Luis Osta
Nov 13 at 17:59




is this what you're looking for: stackoverflow.com/questions/24502898/….
– Luis Osta
Nov 13 at 17:59












thank you for your information, but this is not the answer, this is not about modifying the existing div.
– fong lee
Nov 15 at 14:44




thank you for your information, but this is not the answer, this is not about modifying the existing div.
– fong lee
Nov 15 at 14:44












1 Answer
1






active

oldest

votes


















1














First you need to understand React's philosophy. I strongly suggest you read the official React tutorial: https://reactjs.org/tutorial/tutorial.html.
In this case you would like to appear and disappear a div, this can be accomplished by changing internal state of a React Component. Then you need to create a function that is bound to the context of the instance of the Component.
For example:



class Example extends Component {
constructor(props) {
super(props);
this.state = {
open: true,
};
}

toggle() {
const { open } = this.state;
this.setState({
open: !open,
});
}

render() {
const { open } = this.state;
return (
<main>
<button onClick={this.toggle.bind(this)}>{open ? 'Hide' : 'Show'}</button>
{open && <div id="example"><h1>this is example</h1></div>}
</main>
);
}
}


Here's a codepen: https://codepen.io/anon/pen/PxWdZK?editors=1010






share|improve this answer





















  • thank you for your information, maybe i did not describe correctly, so it should be something like this: <div id="example">example</div> <div id="root"></div>, so how can i hide and show the first "example" in the class that "root" has ?
    – fong lee
    Nov 15 at 14:47












  • why would you need React then ? Just do some: document.querySelector('#example').style.display = 'none';
    – giwiro
    Nov 15 at 17:08








  • 1




    Thank you so much ^_^, i find out how to do it from your tip.
    – fong lee
    Nov 23 at 18:10











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286046%2fhow-to-modify-an-existing-div-element-in-html-by-react%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














First you need to understand React's philosophy. I strongly suggest you read the official React tutorial: https://reactjs.org/tutorial/tutorial.html.
In this case you would like to appear and disappear a div, this can be accomplished by changing internal state of a React Component. Then you need to create a function that is bound to the context of the instance of the Component.
For example:



class Example extends Component {
constructor(props) {
super(props);
this.state = {
open: true,
};
}

toggle() {
const { open } = this.state;
this.setState({
open: !open,
});
}

render() {
const { open } = this.state;
return (
<main>
<button onClick={this.toggle.bind(this)}>{open ? 'Hide' : 'Show'}</button>
{open && <div id="example"><h1>this is example</h1></div>}
</main>
);
}
}


Here's a codepen: https://codepen.io/anon/pen/PxWdZK?editors=1010






share|improve this answer





















  • thank you for your information, maybe i did not describe correctly, so it should be something like this: <div id="example">example</div> <div id="root"></div>, so how can i hide and show the first "example" in the class that "root" has ?
    – fong lee
    Nov 15 at 14:47












  • why would you need React then ? Just do some: document.querySelector('#example').style.display = 'none';
    – giwiro
    Nov 15 at 17:08








  • 1




    Thank you so much ^_^, i find out how to do it from your tip.
    – fong lee
    Nov 23 at 18:10
















1














First you need to understand React's philosophy. I strongly suggest you read the official React tutorial: https://reactjs.org/tutorial/tutorial.html.
In this case you would like to appear and disappear a div, this can be accomplished by changing internal state of a React Component. Then you need to create a function that is bound to the context of the instance of the Component.
For example:



class Example extends Component {
constructor(props) {
super(props);
this.state = {
open: true,
};
}

toggle() {
const { open } = this.state;
this.setState({
open: !open,
});
}

render() {
const { open } = this.state;
return (
<main>
<button onClick={this.toggle.bind(this)}>{open ? 'Hide' : 'Show'}</button>
{open && <div id="example"><h1>this is example</h1></div>}
</main>
);
}
}


Here's a codepen: https://codepen.io/anon/pen/PxWdZK?editors=1010






share|improve this answer





















  • thank you for your information, maybe i did not describe correctly, so it should be something like this: <div id="example">example</div> <div id="root"></div>, so how can i hide and show the first "example" in the class that "root" has ?
    – fong lee
    Nov 15 at 14:47












  • why would you need React then ? Just do some: document.querySelector('#example').style.display = 'none';
    – giwiro
    Nov 15 at 17:08








  • 1




    Thank you so much ^_^, i find out how to do it from your tip.
    – fong lee
    Nov 23 at 18:10














1












1








1






First you need to understand React's philosophy. I strongly suggest you read the official React tutorial: https://reactjs.org/tutorial/tutorial.html.
In this case you would like to appear and disappear a div, this can be accomplished by changing internal state of a React Component. Then you need to create a function that is bound to the context of the instance of the Component.
For example:



class Example extends Component {
constructor(props) {
super(props);
this.state = {
open: true,
};
}

toggle() {
const { open } = this.state;
this.setState({
open: !open,
});
}

render() {
const { open } = this.state;
return (
<main>
<button onClick={this.toggle.bind(this)}>{open ? 'Hide' : 'Show'}</button>
{open && <div id="example"><h1>this is example</h1></div>}
</main>
);
}
}


Here's a codepen: https://codepen.io/anon/pen/PxWdZK?editors=1010






share|improve this answer












First you need to understand React's philosophy. I strongly suggest you read the official React tutorial: https://reactjs.org/tutorial/tutorial.html.
In this case you would like to appear and disappear a div, this can be accomplished by changing internal state of a React Component. Then you need to create a function that is bound to the context of the instance of the Component.
For example:



class Example extends Component {
constructor(props) {
super(props);
this.state = {
open: true,
};
}

toggle() {
const { open } = this.state;
this.setState({
open: !open,
});
}

render() {
const { open } = this.state;
return (
<main>
<button onClick={this.toggle.bind(this)}>{open ? 'Hide' : 'Show'}</button>
{open && <div id="example"><h1>this is example</h1></div>}
</main>
);
}
}


Here's a codepen: https://codepen.io/anon/pen/PxWdZK?editors=1010







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 at 18:04









giwiro

12116




12116












  • thank you for your information, maybe i did not describe correctly, so it should be something like this: <div id="example">example</div> <div id="root"></div>, so how can i hide and show the first "example" in the class that "root" has ?
    – fong lee
    Nov 15 at 14:47












  • why would you need React then ? Just do some: document.querySelector('#example').style.display = 'none';
    – giwiro
    Nov 15 at 17:08








  • 1




    Thank you so much ^_^, i find out how to do it from your tip.
    – fong lee
    Nov 23 at 18:10


















  • thank you for your information, maybe i did not describe correctly, so it should be something like this: <div id="example">example</div> <div id="root"></div>, so how can i hide and show the first "example" in the class that "root" has ?
    – fong lee
    Nov 15 at 14:47












  • why would you need React then ? Just do some: document.querySelector('#example').style.display = 'none';
    – giwiro
    Nov 15 at 17:08








  • 1




    Thank you so much ^_^, i find out how to do it from your tip.
    – fong lee
    Nov 23 at 18:10
















thank you for your information, maybe i did not describe correctly, so it should be something like this: <div id="example">example</div> <div id="root"></div>, so how can i hide and show the first "example" in the class that "root" has ?
– fong lee
Nov 15 at 14:47






thank you for your information, maybe i did not describe correctly, so it should be something like this: <div id="example">example</div> <div id="root"></div>, so how can i hide and show the first "example" in the class that "root" has ?
– fong lee
Nov 15 at 14:47














why would you need React then ? Just do some: document.querySelector('#example').style.display = 'none';
– giwiro
Nov 15 at 17:08






why would you need React then ? Just do some: document.querySelector('#example').style.display = 'none';
– giwiro
Nov 15 at 17:08






1




1




Thank you so much ^_^, i find out how to do it from your tip.
– fong lee
Nov 23 at 18:10




Thank you so much ^_^, i find out how to do it from your tip.
– fong lee
Nov 23 at 18:10


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286046%2fhow-to-modify-an-existing-div-element-in-html-by-react%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)