reactjs - 第二次点击react js时侧边栏没有更新
问题描述
我的要求是,当我单击表格行中的名称时,相应的详细信息应显示在侧栏中。这在第一次尝试时工作正常,但要求是单击下一个条目应更新侧边栏,而无需手动刷新页面。另外,我注意到,一旦侧边栏打开(第一次尝试),在手动刷新页面之前,我无法单击表中的任何条目。
请帮忙。
我正在使用“react-sidebar”中的侧边栏
以下是 App.js
import React, {
Component
} from 'react';
import Sidebar1 from "./Sidebar1";
import './App.css'
class App extends Component {
constructor(props) {
super(props);
this.state = { //state is by default an object
students: [{
id: 1,
name: 'Wasif',
age: 21,
email: 'wasif@email.com'
},
{
id: 2,
name: 'Ali',
age: 19,
email: 'ali@email.com'
},
{
id: 3,
name: 'Saad',
age: 16,
email: 'saad@email.com'
},
{
id: 4,
name: 'Asad',
age: 25,
email: 'asad@email.com'
},
{
id: 5,
name: 'kiwi',
age: 20,
email: 'kiwi@email.com'
}
],
isAboutVisible: false,
selectedStudent: ''
}
}
handleClick(items) {
this.setState({
isAboutVisible: false
});
this.state.students.map(student => {
//const { id, name, age, email } = student;
if (items === student.id) {
this.setState({
isAboutVisible: true,
selectedStudent: student
});
}
return student;
});
}
renderTableData() {
return this.state.students.map((student) => {
const {
id,
name
} = student //destructuring
return ( <
tr onClick = {
() => {
this.handleClick(student.id)
}
}
key = {
id
} >
<
td > {
name
} < /td>
<
/tr>
)
})
}
render() {
return (
<
div >
<
h1 id = 'title' > React Dynamic Table < /h1>
<
table id = 'students' >
<
tbody > {
this.renderTableData()
} <
/tbody> < /
table >
{
this.state.isAboutVisible ? < Sidebar1 abc = {
this.state.selectedStudent
}
handleClose = {
this.handleClose
}
/> : null } < /
div >
);
}
}
export default App;
以下是Sidebar1。
import React from 'react';
import Sidebar from "react-sidebar";
import './Sidebar1.css'
class Sidebar1 extends React.Component {
constructor(props) {
super(props);
this.state = {
sidebarOpen: true,
};
this.onSetSidebarOnpen = this.onSetSidebarOpen.bind(this);
}
onSetSidebarOpen(open) {
this.setState({
sidebarOpen: open
});
}
render() {
return (
<div className="help">
<Sidebar sidebar={<div><p>Name : {this.props.abc.name}</p><p> Age : {this.props.abc.age}</p> <p>Email : {this.props.abc.email}</p></div> }
rootClassName="xyz"
sidebarClassName="imp"
open={this.state.sidebarOpen}
onSetOpen={this.onSetSidebarOpen}
pullRight
styles={{ sidebar: { background: "beige",width:300,paddingLeft :5},overlay : {width:0} }}
>
</Sidebar>
</div>
);
}
}
export default Sidebar1
解决方案
您是否尝试过优化您的handleClick
功能 - :
handleClick(items){
this.setState({
isAboutVisible:true,
selectedStudent: this.state.students.find(student => items === student.id)
});
}
同样为了理智,我们可以添加此条件,以便Sidebar1
每次状态selectedStudent
更改时都会刷新组件。
(this.state.selectedStudent && <Sidebar1 abc={this.state.selectedStudent} handleClose={this.handleClose}/>)
根本原因是抽屉的宽度对你来说是个障碍,我们需要SideBar
像这样为组件设置样式-:
<Sidebar
sidebar={
<div>
<p>Name : {this.props.abc.name}</p>
<p> Age : {this.props.abc.age}</p>{" "}
<p>Email : {this.props.abc.email}</p>
</div>
}
rootClassName="xyz"
sidebarClassName="imp"
open={this.state.sidebarOpen}
onSetOpen={this.onSetSidebarOpen}
pullRight
styles={{
sidebar: {
background: "beige",
width: 300,
paddingLeft: 5,
},
overlay: { width: 0 },
root: { left: "none", width: "60%" },
}}
>
</Sidebar>
基本上我们需要root: {left: "none", width: "60%"}
在styles
推荐阅读
- android - 通过 Google Play 商店 Alpha 测试进行树脂安装时,Flutter 应用程序包未更新
- c++ - 模板的实例化
- php - Laravel 将记录插入到具有第三列的数据透视表中
- delphi - Delphi XE6 FireDac Param 值中文字符串使用属性赋值(变体类型)
- excel - 如何从输入框中复制值并使用它在其他 excel 中搜索相同的值?
- python - numpy 3D 数组根据行顺序重塑/展平为 1D 数组
- javascript - 如何在 javascript 中检查第三方 cookie 是否被阻止
- c# - 从动态服务器抓取 html 列表数据
- node.js - Crud GET:反应 js axios
- jenkins - 在詹金斯完成工作 A 后,如何触发工作 B?