how to call a function from another component in react

user6596773

I am appending a text box on click of Add New Candidate button and I also want to call validate the function of NewCandidate component on the click of save button I have tried with the following code but it's throwing an error if anybody knows the solution please answer.........................................................................................................................................

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Hello React!</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react-dom.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0-alpha1/JSXTransformer.js"></script>    
    <script src="https://www.gstatic.com/firebasejs/3.9.0/firebase.js"></script>    
      </head>
  <body>
    <div id="root"></div>   
    <script type="text/jsx">
    class NewCandidate extends React.Component{
        validate(){
            alert()
        }       
        render(){
            return(
                <table>
                    <tr>
                        <th colSpan="2">Candidate details</th>
                    </tr>
                    <tr>
                        <th>Name</th><td><input type="text" ref="candName" /></td>
                    </tr>
                </table>
            )
        }
    }
    var CandidateList = [<NewCandidate />];

        class Interview extends React.Component{
            constructor(props){
                super();
                this.state={candidates:props.candidates}

            }
            updateCandidateList(newCandidate){
                var updatedCandidates=this.state.candidates;
                updatedCandidates.push(newCandidate);
                this.setState({candidates: updatedCandidates})  
            }
            render(){   
                return(
                    <div>
                        <Candidate candidates={this.state.candidates} />
                        <AddNewCandidate candidateList={this.updateCandidateList.bind(this)} />                     
                    </div>              
                )
            }       
        }
        class AddNewCandidate extends React.Component{
            constructor(){
                super()             
            }
            addNewCandidate(e){
                e.preventDefault();             
                this.props.candidateList(<NewCandidate />)
            }   
            render(){
                return(
                    <form>
                        <button onClick={this.addNewCandidate.bind(this)}>Add New Candidate</button>    
                        <button type="button" onClick={NewCandidate.validate.bind(this)}>Save</button>  
                    </form>
                )
            }
        }
        class Candidate extends React.Component{
            constructor(props){
                super(props);
            }
            render(){
                var items=this.props.candidates.map((item)=>{
                    return (<div>{item}</div>)
                });

                return(
                    <div>
                        {items}
                    </div>
                )
            }
        }   
        ReactDOM.render(<Interview candidates={CandidateList}/>,document.getElementById("root"));           
</script>
  </body>
</html>
user-developer

Please check the following working snippet.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Hello React!</title>
    <script src="https://unpkg.com/react@15/dist/react.js"></script>
    <script src="https://unpkg.com/react-dom@15/dist/react-dom.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
    <script src="https://www.gstatic.com/firebasejs/3.9.0/firebase.js"></script>
  </head>
  <body>
    <div id="root"></div>
    <script type="text/babel">

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

          handleChange(e){
            e.preventDefault();
            this.props.handleCandidateChange(e.target.value, this.props.indexVal);
          }

          render(){
            return(
              <div>
                <table>
                    <tbody>
                    <tr>
                        <th colSpan="2">Candidate details</th>
                    </tr>
                    <tr>
                        <th>Name</th><td><input type="text" onChange={this.handleChange}/></td>
                    </tr>
                    </tbody>
                </table>
              </div>
            )
          }
        }


        class Interview extends React.Component {
          constructor(props){
            super(props);
            this.state = {
              candidates: [],
              values: []
            }
            this.addNewCandidate = this.addNewCandidate.bind(this);
            this.handleSave = this.handleSave.bind(this);
            this.handleCandidateChange = this.handleCandidateChange.bind(this);
          }

          handleCandidateChange(value, index) {
            const newValues = [].concat(this.state.values);
            newValues[index] = value;
            this.setState({values: newValues});
          }

          handleSave(e) {
            e.preventDefault();
            this.state.values.forEach((val, index) => {
              alert(`Validate ${index+1} value`);
            })
          }

          addNewCandidate(e) {
            e.preventDefault();
            const candidateList = [].concat(this.state.candidates);
            candidateList.push(<div key={`candidate-${candidateList.length}`}><NewCandidate indexVal={candidateList.length} handleCandidateChange={this.handleCandidateChange}/></div>)
            this.setState({candidates: candidateList});
          }

          render() {
            return (
              <div>
                {this.state.candidates}
                <button type="button" onClick={this.addNewCandidate}>Add New Candidate</button>
                <button type="button" onClick={this.handleSave}>Save</button>
              </div>
            )
          }
        }

        // render Interview component
        ReactDOM.render(<Interview />,document.getElementById("root"));
</script>
  </body>
</html>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to call a function from a component in another component?

From Dev

Call normal javascript function from another document in a react component function

From Dev

How do I call a function from another component

From Dev

React : how to get input value from one component to make the ajax call in another component?

From Dev

Call React Component Function from onclick in dangerouslySetInnerHTML

From Dev

How do you call a component in another component in react?

From Dev

How do you call a component in another component in react?

From Dev

How to pass a function to another react component?

From Dev

how to call a function from another function in Jquery

From Dev

How to call a number from another function into a function?

From Dev

How to call from function to another function

From Dev

How to call a function from within another function?

From Dev

Angular2 how to call function on click event from another component

From Dev

How to call component's function from service?

From Dev

react-native How to call a function from another class wrapped connect() in react-redux?

From Dev

how to call parent function controller from directive react component angularjs + reactjs?

From Dev

EmberJS: How to call a component function from a controller and calculate the component data

From Dev

EmberJS: How to call a component function from a controller and calculate the component data

From Dev

How to call a function from another class in python?

From Dev

How to call a controller function from another controller?

From Dev

How to call function from another form

From Dev

How to call a function from another form

From Dev

How to call a public function from another form

From Dev

How to call a function from another controller in angularjs?

From Dev

How to call a function from another module

From Dev

How to call function from another ViewController

From Dev

How to call function from another class anywhere

From Dev

How to call function from another form

From Dev

PHP how to call function from another class?

Related Related

  1. 1

    How to call a function from a component in another component?

  2. 2

    Call normal javascript function from another document in a react component function

  3. 3

    How do I call a function from another component

  4. 4

    React : how to get input value from one component to make the ajax call in another component?

  5. 5

    Call React Component Function from onclick in dangerouslySetInnerHTML

  6. 6

    How do you call a component in another component in react?

  7. 7

    How do you call a component in another component in react?

  8. 8

    How to pass a function to another react component?

  9. 9

    how to call a function from another function in Jquery

  10. 10

    How to call a number from another function into a function?

  11. 11

    How to call from function to another function

  12. 12

    How to call a function from within another function?

  13. 13

    Angular2 how to call function on click event from another component

  14. 14

    How to call component's function from service?

  15. 15

    react-native How to call a function from another class wrapped connect() in react-redux?

  16. 16

    how to call parent function controller from directive react component angularjs + reactjs?

  17. 17

    EmberJS: How to call a component function from a controller and calculate the component data

  18. 18

    EmberJS: How to call a component function from a controller and calculate the component data

  19. 19

    How to call a function from another class in python?

  20. 20

    How to call a controller function from another controller?

  21. 21

    How to call function from another form

  22. 22

    How to call a function from another form

  23. 23

    How to call a public function from another form

  24. 24

    How to call a function from another controller in angularjs?

  25. 25

    How to call a function from another module

  26. 26

    How to call function from another ViewController

  27. 27

    How to call function from another class anywhere

  28. 28

    How to call function from another form

  29. 29

    PHP how to call function from another class?

HotTag

Archive