简体   繁体   English

Reactjs 如何在 props 中将 arguments 右传给 function

[英]Reactjs How to right pass arguments to function in props

I studying The Odin Project and try to code CV app ( https://www.theodinproject.com/paths/full-stack-javascript/courses/javascript/lessons/cv-application ).我正在研究 The Odin Project 并尝试编写 CV 应用程序代码 ( https://www.theodinproject.com/paths/full-stack-javascript/courses/javascript/lessons/cv-application )。 I complete Bio part (name, phone, email).我完成生物部分(姓名、电话、电子邮件)。 I understand that Job experince and Education components mostly the same.据我所知,工作经验和教育成分基本相同。 So i try to code Job experience component.所以我尝试编写工作体验组件。 In my App component i declare method to add another job (addJob method), and method to update state of App according to data in Job component with right id.在我的 App 组件中,我声明了添加另一个作业的方法(addJob 方法),以及根据 Job 组件中具有正确 ID 的数据更新 App state 的方法。 To update this data in app i specified unique id to each job component.为了在应用程序中更新此数据,我为每个作业组件指定了唯一的 ID。 What is the best practise to code handleJobChange method?编写 handleJobChange 方法的最佳做法是什么?

I try to send data to my parent component from child.我尝试从子组件向我的父组件发送数据。

App Component应用组件

import React from 'react';
import Bio from './components/Bio'
import CV from './components/CV'
import Job from './components/Job'

class App extends React.Component {
  constructor() {
    super()

    this.handleBioChange = this.handleBioChange.bind(this)
    this.idGen = this.idGen.bind(this)
    this.handleJobChange = this.handleJobChange.bind(this)

    this.state = {
      bio_state: {name:'', phone:'', email:''},
      job: [{id:this.idGen(), company:'', position:'',tasks:'', start:'', until:''}]

    }
  }
  
  idGen() {
     return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(2, 10);
  }

  handleBioChange = (e) => {
    this.setState({
      bio_state: Object.assign(this.state.bio_state,{[e.target.name]: e.target.value})
    })
  }

  addJob = () => {
    this.setState(this.state.job.concat([
      {id:this.idGen(), company:'', position:'',tasks:'', start:'', until:''}
    ]))
  }

  handleJobChange = (e, id) => {
    this.setState(previousState => {
      console.log(e.target)
      console.log("id is ",id)
      let job = [...previousState.job]
      console.log(job)
      let indexOfJob = job.findIndex(job => job.id === id)
      console.log(indexOfJob)
      job[indexOfJob] = {...job[indexOfJob], [e.target.name]: e.target.value}
      return { job }
    })
  }

  render() {
    console.log('app state', this.state.bio_state)
    return (
      <div>
        <Bio onBioChange={this.handleBioChange} />
        {
            this.state.job.map((item) => (
                <Job id={item.id} onJobChange={this.handleJobChange}/>
            ))
        }
        <CV name={this.state.bio_state.name} phone={this.state.bio_state.phone} email={this.state.bio_state.email} jobs={this.state.job}/>
      </div>
    )
  }
}

child component子组件

import React from 'react'

class Job extends React.Component{
  constructor(props){
    super(props)
  }

  render(){
    const id = this.props.id
    return(
      <div id={this.props.id}>
        <label for='company'>Company:</label>
        <input name='company' onChange={(id=this.props.id)=>this.props.onJobChange(id)}></input>
        
        <label for='position'>Position:</label>
        <input name='position' onChange={()=>this.props.onJobChange(id)}></input>
        
        <label for='tasks'>Tasks:</label>
        <input name='tasks' onChange={()=>this.props.onJobChange(this.props.id)}></input>
        
        <label for='start'>Date from:</label>
        <input name='start' onChange={()=>this.props.onJobChange(this.props.id)}></input>
        
        <label for='until'>Date until:</label>
        <input name='until' onChange={()=>this.props.onJobChange(this.props.id)}></input>
      </div>
    )
  }
}

export default Job

I dont know what to do, seems like I incorrect use function in my child component with parameters.我不知道该怎么做,好像我在带参数的子组件中错误地使用了 function。 Please help?请帮忙? I'am desperate =(我很绝望=(

I figure it out.我弄明白了。 I change my app component handleJobChange method like so我像这样更改我的应用程序组件 handleJobChange 方法

handleJobChange = (id, key,value) => {
    this.setState(previousState => {
      console.log("id is ", id)
      console.log("key is ", key)
      console.log("val is ", value)
      let job = [...previousState.job]
      console.log(job)
      let indexOfJob = job.findIndex(job => job.id === id)
      console.log(indexOfJob)
      job[indexOfJob] = {...job[indexOfJob], [key]: value}
      return { job }
    })
  }

And in my Job component i change like so在我的工作组件中,我像这样改变

class Job extends React.Component{
  constructor(props){
    super(props)
    this.handleChange = this.handleChange.bind(this)
  }

  handleChange=(e)=>{
    const key = e.target.name
    const value = e.target.value
    const id = this.props.id

    this.props.onJobChange(id, key, value)
  }

  render(){
    const id = this.props.id
    return(
      <div id={this.props.id}>
        <label for='company'>Company:</label>
        <input name='company' onChange={this.handleChange}></input>
        
        <label for='position'>Position:</label>
        <input name='position' onChange={this.handleChange}></input>
        
        <label for='tasks'>Tasks:</label>
        <input name='tasks' onChange={this.handleChange}></input>
        
        <label for='start'>Date from:</label>
        <input name='start' onChange={this.handleChange}></input>
        
        <label for='until'>Date until:</label>
        <input name='until' onChange={this.handleChange}></input>
      </div>
    )
  }
}

And its worked !它的工作!

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

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