简体   繁体   English

在其他组件中使用 React state

[英]Using React state in other components

I'm having trouble accessing the state stored in my App.js file in another component, Capacity.js.我无法访问存储在我的 App.js 文件中的另一个组件 Capacity.js 中的 state。

I followed the official React documentation to move state up, as I will need it in other components.我按照 React 官方文档将 state 向上移动,因为我将在其他组件中需要它。

Here's my App.js file:这是我的 App.js 文件:

import React, { Component } from 'react'
import Cylinders from './Cylinders'
import Capacity from './Capacity'

class App extends Component {
  constructor(props) {
    super(props);
    this.handleCapacityChange = this.handleCapacityChange.bind(this);
    this.state = {
      capacity: 2
    }
  }

  handleCapacityChange(capacity){
    this.setState({capacity})
  }

  render() {
    return (
      <div className="App">
        <h1>Engine power</h1>
        Select the number of cylinders:
        <Cylinders/>
        Enter the capacity:
        <Capacity
        onCapacityChange={this.handleCapacityChange} />
      </div>
    )
  }
}

export default App

And here is my Capacity.js file:这是我的 Capacity.js 文件:

import React, { Component } from 'react'

class Capacity extends Component {
  constructor(props) {
    super(props);
  }

  onInput() {
    var input = document.getElementById("capinp");
    var cap = input.value;
    this.props.onCapacityChange(parseFloat(cap))
}

  render() {
   return (
     <form name="capacity">
     <input id="capinp" type="range" min="1" max="7" step="0.1" defaultValue="2" onInput={this.onInput.bind(this)}/>
     </form>
  )
 }
}

export default Capacity

Now my understanding from what I have read is that I should be able to use {this.props.capacity} within the render function of Capacity.js like below.现在,我从阅读的内容中了解到,我应该能够在 Capacity.js 的渲染 function 中使用 {this.props.capacity},如下所示。

  render() {
   return (
     <form name="capacity">
     <input id="capinp" type="range" min="1" max="7" step="0.1" defaultValue="2" onInput={this.onInput.bind(this)}/>
     {this.props.capacity}
     </form>
  )
 }

This isn't recognised and doesn't display anything.这无法识别,也不会显示任何内容。 I am new to React so I apologise if I have done something glaringly obvious or fundamentally wrong.我是 React 的新手,所以如果我做了一些明显或根本错误的事情,我深表歉意。

You will need to pass the capacity state from the parent component to the Capacity component as part of its props.您需要将capacity state 作为 props 的一部分从父组件传递到Capacity组件。

render() {
  const { capacity } = this.state
  return (
     // others
     <Capacity
       capacity={capacity}
       onCapacityChange={this.handleCapacityChange} 
    />
  )

}

And on your Capacity component,在您的Capacity组件上,

render() {
  const { capacity } = this.props;

  return (
    <form name="capacity">
      <input id="capinp" type="range" min="1" max="7" step="0.1" defaultValue="2" onInput={this.onInput.bind(this)}/>
      {capacity}
    </form>
  )
}

Send capacity from App.js to Capacity.js将容量从 App.js 发送到 Capacity.js

       <Capacity
        capacity={this.state.capacity}
        onCapacityChange={this.handleCapacityChange} 
       />

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

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