[英]How to fix onClick element. ReactJS
I new in ReactJS and i have one few question.我是 ReactJS 新手,我有几个问题。 I defined function
showModal
and but console.log()
and this.setState({show:!this.state.show});
我定义了函数
showModal
和console.log()
和this.setState({show:!this.state.show});
. .
And after that I applied this function onClick event for div element inside map function.之后,我将这个函数 onClick 事件应用于 map 函数内的 div 元素。
1st question: When I click on div element showModal
work but in console I don't see my console.log
.第一个问题:当我点击 div 元素
showModal
工作但在控制台中我没有看到我的console.log
。
2nd question: I want to make when you click on one div element it must add/show few new div elements but only for one div element (on which I clicked).第二个问题:我想当你点击一个 div 元素时,它必须添加/显示几个新的 div 元素,但只针对一个 div 元素(我点击了)。 But now when I click on one div element it add/show new elements for all div elements which had this
showModal
function.但是现在当我单击一个 div 元素时,它会为所有具有此
showModal
函数的 div 元素添加/显示新元素。
How can i fix this我怎样才能解决这个问题
import React, { Component } from "react";
import Modal from '../components/modal/form'
const DEFAULT_QUERY = 'redux';
const PATH_BASE = 'URL which work correct';
class Actions extends React.PureComponent{
constructor(){
super();
this.state = {
result:null,
show:false,
helpId:null
};
this.setSearchTopStories = this.setSearchTopStories.bind(this);
this.showModal = this.showModal.bind(this);
this.handleClickFromParent = this.handleClickFromParent.bind(this);
this.onClose = this.onClose.bind(this);
}
onClose = e => {
this.setState({ show: false});
}
handleClickFromParent = e => {
this.setState({show: !this.state.show});
}
showModal = e => {
console.log('BABE');
this.setState({show: !this.state.show})
};
setSearchTopStories(result) {
this.setState({ result });
};
componentDidMount() {
fetch(`${PATH_BASE}`)
.then(response => response.json())
.then(result => this.setSearchTopStories(result))
.catch(error => error);
};
render(){
const { searchTerm, result } = this.state;
console.log('* Actions Pure*');
console.log(result);
console.log('=');
return(
<div>
{
(result !== null) ?
result.map(
(item,index) =>
<div>
<div onClick={()=>this.showModal()}>{item.name}</div>
<Modal
id = {index}
handleClickFromParent {this.handleClickFromParent}
item = {[item]}
show = {this.state.show}
onClose = {this.onClose}>
YOLO
</Modal>
</div>
)
: null
}
</div>
)
}
}
export default Actions;
While selecting u can pass the item on method, and on click u can set the item value.选择时可以通过方法传递项目,单击时可以设置项目值。 Please check the below code.
请检查以下代码。
Demo: https://codesandbox.io/s/stackoverflowmodal-19i36演示: https : //codesandbox.io/s/stackoverflowmodal-19i36
this.state = {
result: null,
show: false,
selectedItem:null,
helpId: null
};
// //
showModal = (selectedItem) => {
this.setState({
show: !this.state.show,
selectedItem
});
};
// //
class Actions extends React.PureComponent {
constructor() {
super();
this.state = {
result: null,
show: false,
selectedItem: null,
helpId: null
};
this.setSearchTopStories = this.setSearchTopStories.bind(this);
this.showModal = this.showModal.bind(this);
this.handleClickFromParent = this.handleClickFromParent.bind(this);
this.onClose = this.onClose.bind(this);
}
onClose = e => {
this.setState({
show: false
});
};
handleClickFromParent = e => {
this.setState({
show: !this.state.show
});
};
showModal = selectedItem => {
this.setState({
show: !this.state.show,
selectedItem
});
};
setSearchTopStories(result) {
this.setState({ result });
}
componentDidMount() {
fetch(`${PATH_BASE}`)
.then(response => response.json())
.then(result => this.setSearchTopStories(result))
.catch(error => error);
}
render() {
const { searchTerm, result, selectedItem } = this.state;
return (
<div>
{result && result.length
? result.map((item, index) => (
<div>
<div onClick={() => this.showModal(item)}>{item.name}</div>
</div>
))
: null}
{selectedItem && (
<Modal
id={index}
handleClickFromParent={this.handleClickFromParent}
item={[selectedItem]}
show={this.state.show}
onClose={this.onClose}
>
YOLO
</Modal>
)}
</div>
);
}
}
export default Actions;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.