Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React JS reference function in another component

I'm trying to get a button rendered through another component to reference and/or influence the state of a different component.

var Inputs = React.createClass({
  getInitialState: function(){
    return {count: 1};
  },
  add: function(){
    this.setState({
      count: this.state.count + 1
    });
  },
  render: function(){
    var items = [];
    var inputs;
      for (var i = 0; i < this.state.count; i++){
        items.push(<input type="text" name={[i]} />);
        items.push(<br />);
      }
    return (
      <div className="col-md-9">
        <form action="/" method="post" name="form1">
          {items}
          <input type="submit" className="btn btn-success" value="Submit Form" />
        </form>
      </div>
     );
   }
 });

I want to write a new component that will be able to access the add function in Inputs. I tried to reference it directly with Inputs.add like this:

var Add = React.createClass({
  render: function(){
    return (
      <input type="button" className="btn" value="Add an Input" onClick={Inputs.add} />
    );
  }
});

But that didn't work. How would I be able to access a component's functions through another component, or influence the state of a component through another component? Thanks.

like image 253
user2829448 Avatar asked Jan 10 '14 21:01

user2829448


People also ask

How do you call a function in another functional component React?

js do <Button onPress={movePopUpCard}></Button> . With calling the function directly, the return value of the function will be passed to onPress .

How do you call a function from one component to another in react JS?

Second Component class PopupOver extends React. Component{ constructor(){ super(); // here i need to call Header class function check click.... // How to call Header. checkClick() from this class } render(){ return ( <div className="displayinline col-md-12 "> Hello </div> ); } } export default PopupOver; reactjs.

How do you pass a ref to another component?

We pass our ref down to <FancyButton ref={ref}> by specifying it as a JSX attribute. React passes the ref to the (props, ref) => ... function inside forwardRef as a second argument. We forward this ref argument down to <button ref={ref}> by specifying it as a JSX attribute.

How do you export a function from one component to another in React?

Use named exports to export a component in React, e.g. export function Button() {} . The exported component can be imported by using a named import as import {Button} from './another-file. js' . You can use as many named exports as necessary in a file.


2 Answers

You could accomplish this by creating a parent component that is responsible for managing the state and then just push the state down to the sub-components as props.

/** @jsx React.DOM */

var Inputs = React.createClass({

    render: function () {
        var items = [];
        var inputs;
        for (var i = 0; i < this.props.count; i++) {
            items.push( <input type="text" name={[i]} />);
            items.push(<br />);
        }
        return ( 
            <div className = "col-md-9"> 
                <form action = "/" method = "post" name = "form1"> 
                    {items} 
                    <input type="submit" className="btn btn-success" value = "Submit Form" />            
                </form>
            </div> 
       );
    }
});

var Add = React.createClass({
    render: function () {
        return (<input type = "button" className="btn" value="Add an Input" onClick={this.props.fnClick}/> );
  }
});

var Parent = React.createClass({
    getInitialState: function(){
        return {count:1}
    },
    addInput: function(){
        var newCount = this.state.count + 1;
        this.setState({count: newCount});
    },
    render: function(){
        return (
            <div>
                <Inputs count={this.state.count}></Inputs>
                <Add fnClick={this.addInput}/>
            </div>
        );
    }
});

React.renderComponent(<Parent></Parent> , document.body);

jsFiddle

like image 180
Markus-ipse Avatar answered Sep 26 '22 18:09

Markus-ipse


You can call functions on the return value of renderComponent:

var Inputs = React.createClass({…});
var myInputs = React.renderComponent(Inputs);
myInputs.add();

The only way to get a handle to a React Component instance outside of React is by storing the return value of React.renderComponent. Source.

like image 23
Sjoerd Avatar answered Sep 23 '22 18:09

Sjoerd