[英]How can I set another state to pass data to from the initial state in a react class component
can anyone help me out and explain how I can set another state in this component:谁能帮我解释一下如何在这个组件中设置另一个 state:
export class MapContainer extends Component {
constructor(props) {
super(props);
this.state = {
attorneys: []
}
}
componentDidMount() {
fetch('**********')
.then((response) => response.json())
.then(data => {
this.setState({ attorneys: data });
});
}
displayMarkers = () => {
return this.state.attorneys.map((attorney) => {
return <Marker key={attorney.id} id={attorney.id} position={{
lat: attorney.latitude,
lng: attorney.longitude
}}
onClick={() => {
}}
/>
})
}
render() {
return (
<div style={{position:'absolute', left: 450
}}>
<Map
google={this.props.google}
zoom={8}
style={{width:'50vw', height:'80vh'}}
initialCenter={{lat: 38.907192, lng: -77.036873}}
loadingElement={<div style={{height:"100%"}} />}
containerElement ={<div style={{height:"100%"}} />}
mapElement={<div style={{height:"100%"}} />}
>
{this.displayMarkers()}
</Map>
<div style={{position:'relative',top:800, right:200, paddingTop:25}}>
<Link to={``} className="btn btn-sm btn-success mb-2">Add Attorney</Link>
<input class="form-control" style={{ marginBottom: 15, marginTop: 5 }} id="txt_query" type="text" placeholder="Seach here..." />
<table className="table table-bordered table-hover table-hover-cursor">
<thead>
<tr >
<th style={{ width: '10%', cursor: "pointer" }}>Attorney Name</th>
<th style={{ width: '12%', cursor: "pointer" }}>Email</th>
<th style={{ width: '40%' }}>Phone Number</th>
<th style={{ width: '40%' }}>Indiviual Address</th>
<th style={{ width: '10%' }}>Admitted</th>
<th style={{ width: '10%' }}>Practice area</th>
<th style={{ width: '10%' }}>Month/Year Joined</th>
<th style={{ width: '10%' }}>View</th>
</tr>
</thead>
<tbody class="searchable">
{this.state.attorneys.map((attorney) =>
<tr key={attorney.id}>
<td>{attorney.name}</td>
{/* <td>{case_.case_id}</td> */}
<td>{attorney.email}</td>
<td> {attorney.phone}</td>
<td> {attorney.street_address}</td>
<td>
{attorney.admitted}
</td>
<td>{attorney.practice_area}</td>
<td>{attorney.month_year_joined}</td>
<td style={{ whiteSpace: 'nowrap' }}>
Basically I want to set another state like:基本上我想设置另一个 state 像:
const [selectedAtt, setSelectedAtt] = useState(null);
to then use the setter setSelectedAtt to grab the attorney data on an onclick function like so:然后使用 setter setSelectedAtt 来获取 onclick function 上的律师数据,如下所示:
onClick={() => {
setSelectedAtt(attorney);
}}
then from there I was going to use the state selectedAtt to display an info window on click passing data from the clicked attorney.然后从那里我将使用 state selectedAtt 来显示有关点击律师的点击传递数据的信息 window。
However I get the error: React Hook "useState" cannot be called in a class component.但是我收到错误:无法在 class 组件中调用 React Hook "useState"。 React Hooks must be called in a React function component or a custom React Hook function
必须在 React function 组件或自定义 React Hook function 中调用 React Hooks
Can i use multiple states in this.state?我可以在 this.state 中使用多个状态吗? I guess I'm just unsure how to use multiple states in a component class.
我想我只是不确定如何在组件 class 中使用多个状态。 Any advice would be awesome
任何建议都会很棒
With react class component, you can write your own state setter with spread syntax which can copy the prvious state.使用 react class 组件,您可以编写自己的 state 设置器,其扩展语法可以复制以前的 state。
constructor(props) {
super(props);
this.state = {
attorneys: [],
selectedAtt: null,
};
}
setAttorneys = (data) =>
this.setState((prev) => ({...prev, attorneys: data}));
setSelectedAtt = (att) =>
this.setState((prev) => ({...prev, selectedAtt: att}));
render() {
console.log(this.state);
return <div onClick={() => this.setSelectedAtt('test')}>test</div>;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.