[英]How to fix: How to show state with onClick to div?(React)
I have sidebar with document types on it(docs, table, slider, html ..). 我在工具栏上有文档类型(文档,表格,滑块,html ..)。 I want that, if i click on docs element it will show docs in another div like a header.
我想要,如果我单击docs元素,它将在另一个div中显示文档,例如标题。
I have 3 files: DocumentType.tsx, Sidebar.tsx and Results.tsx 我有3个文件:DocumentType.tsx,Sidebar.tsx和Results.tsx
In DocumentType.tsx: 在DocumentType.tsx中:
import React from 'react';
const documentType = (props ) =>{
return(
<div>
<p id="fileType">{props.type}</p>
</div>
)
};
export default documentType;
In Sidebar.tsx: 在Sidebar.tsx中:
typeState = {
documentTypes: [
{ type: "Dokumendid" },
{ type: "PDF" },
]
}
toDocument = () => {
this.setState({
documentTypes: [
{ type: "Dokumendid" }
console.log("Document was clicked");
]
})
}
toPdf = () => {
this.setState({
documentTypes: [
{ type: "Pdf" }
console.log("PDF was clicked")
]
})
}
render(){
return(
<a className="a" href="/search?filter%3Atype=doc" onClick={this.toDocument}>
<div className="icons dokument">
<img src={dokument} alt="dokument"/>
<a className="title">dokument</a>
</div>
</a>
<a className="a" href="/search?filter%3Atype=pdf" onClick={this.toPdf}>
<div className="icons pdf">
<img src={pdf} alt="pdf"/>
<a className="title">pdf</a>
</div>
</a>
)
}
And in Results.tsx: 并在Results.tsx中:
... ...
<DocumentType />
.. ..
If I understood your question correctly.. you wanted to show data in a div when onClick event triggers.. 如果我正确理解了您的问题,那么您希望在onClick事件触发时在div中显示数据。
lets say your state object has 可以说您的状态对象有
state = {
data: ''
}
//clicked function //单击函数
clicked =() => {
this.setState({data: 'clickedme'})
}
div element: <div onClick={this.clicked} >{this.state.data}</div>
div元素:
<div onClick={this.clicked} >{this.state.data}</div>
simple example when an onClick
event occurs a div
and displaying the state data
object.. 一个简单的示例,当
onClick
事件发生div
并显示状态data
对象时。
You want to show a document type in Results
component when a document in Sidebar
component is clicked. 当单击
Sidebar
组件中的文档时,要在Results
组件中显示文档类型。
You have documentType
state in Sidebar
component and you want to pass it to Results
component. 您在
Sidebar
组件中具有documentType
状态,并且想要将其传递给Results
组件。 So for that you can make Results
component as child component of Sidebar
component and pass the selected document type ie documentType
state as props. 因此,您可以将
Results
组件作为Sidebar
组件的子组件,并将选定的文档类型(即documentType
状态)作为prop传递。
Sidebar.js Sidebar.js
import React, {Component} from 'react'
import Results from 'path-to-results';
class Sidebar extends Component {
state = {
// instead of using "documentType" as array
// you can make it null for initial value
documentType: null
}
// instead of using "toPDF" or "toDocument" method
// you can use single method to update the state
handleDocType = (docType) => {
this.setState({
documentType: docType
})
}
render() {
return (
<div>
// pass "document" as argument to handleDocType method
<a className="a" href="#" onClick={() => this.handleDocType('document')}>
<div className="icons dokument" >
<img src="" alt="dokument"/>
<a className="title">dokument</a>
</div>
</a>
// pass "pdf" as argument to handleDocType method
<a className="a" href="#" onClick={() => this.handleDocType('pdf')}>
<div className="icons pdf">
<img src="" alt="pdf"/>
<a className="title">pdf</a>
</div>
</a>
// checking if "documentType" is null or not
// if it is null nothing is rendered
// if it is not null then "Results" component is rendered
{ this.state.documentType && <Results type={this.state.documentType} /> }
</div>
)
}
}
Results.js Results.js
import React, { Component } from 'react'
import DocType from 'path-to-doctype'
class Results extends Component {
// .... your other codes
render() {
return (
<div>
// ....... your other codes
<DocType type={this.props.type} />
</div>
)
}
}
export default Results
DocType.js DocType.js
import React from 'react';
const DocumentType = (props ) =>{
return(
<div>
<p id="fileType">{props.type}</p>
</div>
)
};
export default DocumentType;
UPDATE UPDATE
If Sidebar
and DocType
components are children components of Results
component then add documentType
state to Results
component and pass documentType
state as props to DocType
component. 如果
Sidebar
和DocType
组件是Results
组件的子组件,则将documentType
状态添加到Results
组件,并将documentType
状态作为DocType
传递给DocType
组件。
Results.js Results.js
class Results extends Component {
// add state "documentType"
state = {
documentType: null
}
// add "handleDocType" method
handleDocType = (docType) => {
this.setState({
documentType: docType
})
}
// .... your other codes
render() {
return (
<div>
// .... your other codes
// pass "handleDocType" as props to Sidebar component
<Sidebar handleDocType={this.handleDocType}/>
// pass "documentType" state as props to DocType component
<DocType type={this.state.documentType} />
</div>
)
}
}
export default Results
Sidebar.js Sidebar.js
class Sidebar extends Component {
// use "docTypeHandler" to call parent "handleDocType" method
// that updates "documentType" state in Results component
docTypeHandler = (doctype) => {
this.props.handleDocType(doctype)
}
render() {
return (
<div>
<a className="a" href="#" onClick={() => this.docTypeHandler('document')}>
<div className="icons dokument" >
<img src="" alt="dokument"/>
<a className="title">dokument</a>
</div>
</a>
<a className="a" href="#" onClick={() => this.docTypeHandler('pdf')}>
<div className="icons pdf">
<img src="" alt="pdf"/>
<a className="title">pdf</a>
</div>
</a>
</div>
)
}
}
export default Sidebar
DocType.js DocType.js
const DocType = (props ) =>{
return(
<div>
<p id="fileType">{props.type}</p>
</div>
)
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.