javascript - 基于用户输入的条件渲染

标签 javascript reactjs components conditional-rendering

我刚刚开始学习 React,并且正在为条件渲染而苦苦挣扎。我想根据表单输入渲染组件,但我不确定需要做什么或需要在哪里执行。

我已经导入了我的 Form 组件,它有我想使用的输入,还有另一个像这样的组件:

import React, {Component} from 'react';
import Form from './Form';
import CardOne from './CardOne';
import CardTwo from './CardTwo';
import CardThree from './CardThree';

export default class CardContainer extends Component {
  render(){
    return (
      <div>
        <CardOne />
        <CardTwo />
        <CardThree />
     </div>
    )
  }
}

如果表单提交时输入值大于 X,我基本上希望能够显示某些卡片,但我不知道如何定位导入的组件。

这是我的表单组件:

export default class Form extends Component {
  state = {
    number: ''
  };

  change = (e) => {
    this.setState({
      [e.target.name]: e.target.value
    });
  };

  onSubmit = e => {
    e.preventDefault();
    this.props.onSubmit(this.state);
    this.setState({
      number: ''
    })
  };

  render(){
    return (
      <form>
        <label>Number</label>
        <input
        type="number"
        name="number"
        placeholder="Number"
        value={this.state.number}
        onChange={e => this.change(e)} />

        <button onClick={e => this.onSubmit(e)}>Submit</button>
      </form>
    )
  }
}

任何帮助将不胜感激!

最佳答案

我已经重新设计了你的表单组件,下面是我的代码。 .如果您在这方面遇到任何问题,请告诉我。

import React, { Component } from 'react';
import CardOne from './CardOne';
import CardTwo from './CardTwo';
import CardThree from './CardThree';

export default class Form extends Component {
  state = {
    number: '',
    showOne:true,
    showTwo:false,
    showThree:false,
    userInputValue:''
  };

  change = (e) => {
    this.setState({
      userInputValue: e.target.value
    });
  };

  onSubmit = e => {
    e.preventDefault();
    this.props.onSubmit(this.state);
    if (this.state.userInputValue > 10 && this.state.userInputValue <20 ){
      this.setState({
        showTwo: true,
      })
    }
    if (this.state.userInputValue > 20 && this.state.userInputValue < 30) {
      this.setState({
        showThree: true,
      })
    }
  };

  render() {
    return (
      <div>
      <form>
        <label>Number</label>
        <input
          type="number"
          name="number"
          placeholder="Number"
          value={this.state.userInputValue}
          onChange={e => this.change(e)} />

        <button onClick={e => this.onSubmit(e)}>Submit</button>
      </form>
      <div>
      {this.state.showOne ? 
            <CardOne />
       :
       <div></div>
      }
      {this.state.showTwo ?
        <CardTwo />
        :
        <div></div>
      }
      {this.state.showThree ?
        <CardThree />
        :
        <div></div>
      }
      </div>
      </div>
    )
  }
}

// What i wrote above is your base functionality . You reedit the condition depends on ur requirement .

关于javascript - 基于用户输入的条件渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50658253/

相关文章:

javascript - 如何将对象加载到 jQuery 对象

javascript - React_Redux : Pass parameter in callback during continually callback function

reactjs - 如何渲染仅显示 SPA 中特定组件的新路线?

javascript - 如何整合 Bootstrap collapse 和 scrollspy?

javascript - 如何编写一个简单的音乐播放器?

javascript - Vue js 带条件的 for 循环

javascript - 将 props 传递给 React 中的子组件

angular - 即时添加/删除组件

.net - 语法高亮 : rich text box control for . NET

angular - 模块 'Ng2SmartTableModule' 声明的意外模块 'AppModule'