简体   繁体   English

如何在反应 class 组件中设置另一个 state 以将数据从初始 state 传递到

[英]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.

相关问题 如何从 API 数据设置初始 React 状态? - How can I set initial React state from API data? 如何在 react 中重定向到另一个组件并传递我在前一个组件中设置的 state? - How can I redirect to another component in react and pass the state that I set in the previous component? 如何在渲染之前将组件初始状态设置为另一个状态? - How can I set a component initial state with another state before render? React / Redux:如何将此状态从一个组件传递到另一个文件? - React/Redux: How can I pass this state from one component to another file? 如何将减速器的初始状态传递给组件? - How can I pass the initial state of a reducer to a component? 如何将 state 从一个组件传递到另一个组件,如何修改 state? - How can I pass the state from one component to another and how can I modify the state? 如何在反应中将组件重置为其初始状态 - How can I reset a component to it's initial state in react 如何在React js中将状态从一个组件传递到另一个组件? - How to pass state from one component to another in React js? 从另一个组件React js设置状态 - Set state from another component React js 在 REACT.js 中,如何将状态从子组件传递到父组件作为道具 - In REACT.js how can I pass a state from child component up to Parent component as a prop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM