I have a parent component with 2 child components, i want to implement a zoom on child component upon a click on a resize button.
i decided to handle everything on the parent, because on resize i want the other child component to not be displayed, here is some code:
parent:
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
editorInput: '',
previewInput: '',
editorZoomed: "false",
previewerZoomed: "false"
};
this.handleEditorResize = this.handleEditorResize.bind(this);
this.handlePreviewerResize = this.handlePreviewerResize.bind(this);
}
componentDidMount() {
this.setState((state, props) => ({ editorInput: document.getElementById("editor").value }));
}
handleEditorResize(event) {
let zoomed = event.target.getAttribute('resized');
if (zoomed === "true") {
document.getElementById("previewer-box").style.removeProperty("display");
this.setState((state, props) => ({ editorZoomed: "false" }));
} else if (zoomed === "false") {
document.getElementById("previewer-box").style.display = "none";
this.setState((state, props) => ({ editorZoomed: "true" }));
}
}
handlePreviewerResize(event) {
let zoomed = event.target.getAttribute('resized');
if (zoomed === "true") {
document.getElementById("editor-box").style.removeProperty("display");
this.setState((state, props) => ({ previewerZoomed: "false" }));
} else if (zoomed === "false") {
document.getElementById("editor-box").style.display = "none";
this.setState((state, props) => ({ previewerZoomed: "true" }));
}
}
render() {
return (
<div className="App">
<Editor
componentName="editor-box"
textChange={this.handleEditorChange}
resize={this.handleEditorResize}
resizeState={this.state.editorZoomed}
/>
<Previewer
componentName="previewer-box"
textChange={this.parseExpression(this.state.editorInput)}
resize={this.handlePreviewerResize}
resizeState={this.state.previewerZoomed}
/>
</div>
);
}
}
child:
class Editor extends React.Component {
constructor(props) {
super(props);
this.state = {
resized: props.resizeState
};
}
render() {
return (
<div id={this.props.componentName}>
<div id="editor-header" className="App-box">
<FontAwesomeIcon icon={faIndent} className="App-box-logo"/>
<h3>Editor</h3>
<button
id="resize-editor"
className="App-box-button"
resized={this.state.resized}
onClick={this.props.resize}><i>resize button logo</i></button>
</div>
<div id="editor-body">
<textarea id="editor" onChange={this.props.textChange} defaultValue="
"
/>
</div>
</div>
)
}
}
now i realize there is some redundancy in the code, this is not what i intend to be the final code, i just want to know where the problem is ?
i want the resize button to be toggled on/off, but in this code it can only be triggered once, when i try to toggle it off it doesn’t work, and the state doesnt change .
any help would be greatly appreciated