简体   繁体   English

从另一个类调用函数(反应)

[英]Calling function from another class (React)

I'm really new to React and JS in general and have been following a tutorial of sorts.总的来说,我对 React 和 JS 真的很陌生,并且一直在关注各种教程。

Upon HandleFormSubmit running, I want to refresh data shown in the Contact Management table, since we've just pushed a bunch of new data.在 HandleFormSubmit 运行时,我想刷新 Contact Management 表中显示的数据,因为我们刚刚推送了一堆新数据。

How could I call fetchData() from within HandleFormSubmit?如何从 HandleFormSubmit 中调用 fetchData()? I'm struggling since is rendered in App.我很挣扎,因为它是在 App 中呈现的。

Thanks谢谢

<script type="text/babel">

class ContactForm extends React.Component { 
    state = { 
        name: '',
        emai: '',
        country: '',
        city: '',
        job: '',
    }
    handleFormSubmit( event ) {
        event.preventDefault(); 

        let formData=new FormData();
        formData.append('name', this.state.name) 
        formData.append('email', this.state.email)
        formData.append('city', this.state.city)
        formData.append('country', this.state.country)
        formData.append('job', this.state.job)

        axios({
            method: 'post',
            url: '/api/contacts.php',
            data: formData,
            config: { headers: {'Content-Type': 'multipat/form-data' }}
        })
        .then(function (response){
            console.log(response)
            this.fetchData();
        })
        .catch(function (response) {
            console.log(response)
        });
    }

   render() { 
    return ( 
        <form> 
            <label>Name</label> 
            <input type="text" name="name" value={this.state.name} onChange={e => this.setState({ name: e.target.value})}/> 
            <label>Email</label>
            <input type="email" name="email" value={this.state.email} onChange={e => this.setState({ email: e.target.value})}/> 
            <label>Country</label>
            <input type="text" name="country" value={this.state.country} onChange={e => this.setState({ country: e.target.value})}/>    
            <label>City</label> 
            <input type="text" name="city" value={this.state.city} onChange={e => this.setState({ city: e.target.value})}/> 
            <label>Job</label>
            <input type="text" name="job" value={this.state.job} onChange={e => this.setState({ job: e.target.value})}/>    
            <input type="submit" onClick={e => this.handleFormSubmit(e)} value="Create Contact" />
        </form>)
    }
}   

class App extends React.Component {
    state = {
        contacts: []
    }
    render() {
      return ( 
        <React.Fragment> 
          <h1>Contact Management</h1> 
          <table border='1' width='100%'>
          <tr> 
            <th>Name</th> 
            <th>Email</th> 
            <th>Country</th>
            <th>City</th> 
            <th>Job</th> 
          </tr>
          {this.state.contacts.map((contact) => (
          <tr>
            <td>{ contact.name }</td>   
            <td>{ contact.email }</td>  
            <td>{ contact.country }</td>    
            <td>{ contact.city }</td>   
            <td>{ contact.job }</td>
          </tr>
          ))}
          </table>
          <ContactForm />
        </React.Fragment>   
    );
        }

    fetchData() {
        const url ='/api/contacts.php'
        axios.get(url).then(response => response.data).then((data) => {
            this.setState({ contacts: data })
            console.log(this.state.contacts) 
        })
    }

    componentDidMount() {
        this.fetchData();
       }
}   


ReactDOM.render(<App />, document.getElementById('root'));  
</script>

You can pass function reference from parent to child, like this您可以将函数引用从父级传递给子级,就像这样

<ContactForm fetchData={this.fetchData}/>

and in parent component you can call that function like this在父组件中,您可以像这样调用该函数

this.props.fetchData()

PS: don't forget to bind function in constructor. PS:不要忘记在构造函数中绑定函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM