简体   繁体   English

在反应中传递输入状态

[英]Passing input state around in react

I have a react class that has this code: 我有一个具有以下代码的React类:

  getInitialState: function(){
    return {
      results: false,
      value: ''
    };
  },

  handleClick: function(event) {
    this.setState({ results: true });
    event.preventDefault();
  },

  handleChange: function(event){
    this.setState({value: event.target.value})
  },

    <input type="text" id="playerName" value={this.state.value} onChange={this.handleChange} placeholder="name" />

IN ANOTHER REACT CLASS 在另一个反应班

I have: 我有:

myFunc: function() {
    this.setState({info: true})
    let name = this.props.value;
    console.log(name) --> returns undefined
  },

How can I name to be equal to the name that the user typed in by passing it down from one react class to another 我如何通过将用户名从一个react类传递到另一类来使其名称等于用户键入的名称

I can post more code but I thought props was the way to pass down code you needed elsewhere. 我可以发布更多代码,但我认为道具是传递其他地方需要的代码的方式。 this.state.value also returns undefined this.state.value也返回未定义

edit: 编辑:

myFunc called here: myFunc在这里打电话:

<input type="submit" value="I can play" onClick={() => this.myFunc()}/>

The easiest way for this to work is to have a container component, this container component would handle all state management and wrap around child components: 最简单的方法是拥有一个容器组件,该容器组件将处理所有状态管理并包装子组件:

<WrapperComponent>
 <Input/>
 <Info />
</WrapperComponent

Your input can still have its own state if you want to do debouncing and things like that but the wrapper component will have its own functions that set its own state and it passes those functions down to input so that input can call them with its new values and then the wrapper component will update and then the Info component will receive its value props from the wrapper component and everything will be in sync. 如果您想进行反跳操作,则输入仍然可以具有自己的状态,但是包装器组件将具有自己的函数来设置自己的状态,并将这些函数向下传递给输入,以便输入可以使用其新值来调用它们然后包装器组件将更新,然后信息组件将从包装器组件接收其价值道具,并且所有组件将同步。

here is an example codepen http://codepen.io/finalfreq/pen/VKPXoN 这是一个示例codepen http://codepen.io/finalfreq/pen/VKPXoN

class Wrapper extends React.Component {    
  constructor() {
  super();
    this.state = {
      value: 'default'
    }
  }

  _handleChange = (value) => {
    this.setState({value: value})  
  };

  render() {
    return (
      <div>
       <Input onChange={this._handleChange} value={this.state.value}/>
       <Info value={this.state.value} />
      </div>
    )
  }
}

class Input extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      value: props.value
    }
  }

  onChange = (e) => {
    this.setState({value: e.target.value});
    this.props.onChange(e.target.value);
  };

  render() {
    return <input type="text" value={this.state.value} onChange={this.onChange} />
  }
}

const Info = (props) => {
  return (
   <h1> {props.value} </h1>
  )
}


ReactDOM.render(<Wrapper />,document.getElementById('app'));

obviously you can easily convert this to using the createClass version doesn't have to be using es6 version. 显然,您可以轻松地将其转换为使用createClass版本,而不必使用es6版本。 The main take away here is that if you want to share values across components that aren't directly related then you definitely are going to want a state container component that is handling everything. 这里的主要收获是,如果您希望在不直接相关的组件之间共享值,那么您肯定会想要一个处理所有内容的状态容器组件。

Here is a great article going over container vs presentational components https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0 这是一篇很棒的文章,介绍了容器和演示组件https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0

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

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