[英]How can I show my react component from hide and re-render again?
Here is my html code 这是我的HTML代码
<div id="btn">Click</div>
<div id="main">
</div>
Here is my component.js file code 这是我的component.js文件代码
class Main extends Component {
constructor(props, context) {
super(props, context);
this.state = {
name : ""
}
}
render(){
<input type="text" className="form-control" id="userName" defaultValue={this.state.name}></input>
}
}
ReactDOM.render(<Main />, document.getElementById('main'));
When I click on #btn, I use javascript to show or hide my #main react component which has a input component and the default state of name is empty string. 当我单击#btn时,我使用JavaScript来显示或隐藏我的#main react组件,该组件具有输入组件,并且名称的默认状态为空字符串。
If I type some value for my input , like 'abc', and then click #btn again to hide #main, then click #btn again to show #main.The 'abc' still there. 如果我为输入内容输入一些值,例如'abc',然后再次单击#btn以隐藏#main,然后再次单击#btn以显示#main.'abc'仍然存在。
How can I have my component re-render base on the default state every time I click #btn to show #main? 每次单击#btn以显示#main时,如何基于默认状态重新渲染组件?
is this what you want? 这是你想要的吗?
class Main extends React.Component { constructor(props, context) { super(props, context); this.state = { name : "" }; } handleInputChange = () => { const { value } = this.refs.name; this.setState({name: value}); }; handleBtnClick = () => { const target = this.refs.name; target.classList.toggle('hidden'); }; render() { return ( <div> <p className="form-control"> <input type="text" id="userName" ref="name" value={this.state.name} onChange={this.handleInputChange} /> </p> <button onClick={this.handleBtnClick}>button</button> </div> ); } } ReactDOM.render( <Main />, document.getElementById('main') );
.hidden { visibility: hidden; }
<div id="main"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
You are mounting your React application at the #main element. 您正在将React应用程序安装在#main元素上。 The React context won't know anything outside of that. React上下文对此一无所知。 If you would like to show or hide components on button click, you should include your #btn element in your React component. 如果您想在单击按钮时显示或隐藏组件,则应在React组件中包含#btn元素。 Something like this should work: 这样的事情应该起作用:
<!-- Your html file -->
<div id="main"></div>
// component.js file
/* Use a wrapper class to control when you want components to render */
class WrapperMain extends Component {
constructor(props) {
super(props);
this.state = { showMain: true };
}
handleBtnClick() {
// Calling setState forces a re-render
this.setState({ showMain: !this.state.showMain });
}
render() {
return (
<div>
{/* Bring your click div into the React context so that it
can better control the rendering of other React components */}
<div id="btn" onClick={this.handleBtnClick.bind(this)}>
Click
</div>
{/* You can use an anonymous function to allow a check for
whether the <Main /> component should be shown or not */}
{(() => {
if (this.state.showMain) {
return <Main />
}
})()}
</div>
)
}
}
class Main extends Component {
constructor(props, context) {
super(props, context);
this.state = { name : "" }
}
render() {
return (
<div>
<input
type="text"
className="form-control"
id="userName"
defaultValue={this.state.name}></input>
</div>
);
}
}
ReactDOM.render(<WrapperMain />, document.getElementById("main"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.