简体   繁体   English

如何在reactjs的渲染函数中有条件地使用开关切换

[英]How to use switch toggle conditionally in render function in reactjs

I'm new to React JS.我是 React JS 的新手。 I have two objects taskData and taskDatafilter.我有两个对象 taskData 和 taskDatafilter。 I want to render two of them based on condition.我想根据条件渲染其中的两个。 The toggle switch if the switch is turned on then only render taskDatafilter but if it is off then rendered taskData.如果开关打开,则切换开关仅呈现 taskDatafilter,但如果关闭则呈现 taskData。 My code so far is到目前为止我的代码是

    import React, { Component } from "react";
import Button from "reactstrap/lib/Button";
import TodoItem from "../TodoItem/TodoItem";
import Switch from '@material-ui/core/Switch';

export default class TodoList extends Component {

    render() {
    const {
      showTaskData,
      clearList,
      handleDelete,
      todoDeleteMsg,
      editTodo,
    } = this.props;
    const isActive = this.props.showTaskData.active;
    let taskData = [];
      let taskDatafilter = [];
    if (showTaskData.length) {
      taskData = showTaskData.map((task) => {
        return (
          <TodoItem
            key={task.id}
            title={task.title}
            description={task.description}
            handleDelete={() => {
              handleDelete(task.id);
            }}
            todoDeleteMsg={todoDeleteMsg}
            editTodo={() => {
              editTodo(task.id, task.title, task.description);
            }}
          />
        );
      });
        taskDatafilter = showTaskData.filter((value)=> value.completed === 1).map((task) => {
            return (
                <TodoItem
                    key={task.id}
                    title={task.title}
                    description={task.description}
                    handleDelete={() => {
                        handleDelete(task.id);
                    }}
                    todoDeleteMsg={todoDeleteMsg}
                    editTodo={() => {
                        editTodo(task.id, task.title, task.description);
                    }}
                />
            );
        });
    }
    return (
      <ul className="list-group my-2">
        <h3 className="text-capitalize">Todo List </h3>
        <div className="d-flex justify-content-between mb-5">
          Task and Description
            <Switch value="on" inputProps={{ 'aria-label': 'Switch A' }}  />{taskDatafilter}
        </div>
        {taskData}
        <Button color="danger" onClick={clearList}>
          Clear all
        </Button>
        <p className="text-danger">{todoDeleteMsg}</p>
      </ul>
    );
  }
}

I am confused to use the switch button with that condition in my code I am badly stuck at this point for 5 hours.我很困惑在我的代码中使用具有该条件的开关按钮我在这一点上严重卡住了 5 个小时。 Basically I am working on todo app and I want to show a switch button on the front end which if on then show completed task else show active tasks基本上我正在开发 todo 应用程序,我想在前端显示一个开关按钮,如果打开则显示已完成的任务,否则显示活动任务

Bound your list items into parent tag like div or React.Fragment将您的列表项绑定到父标签中,如 div 或 React.Fragment

if (showTaskData.length) {
      taskData = return( <div> {showTaskData.map((task) => 
          <TodoItem
            key={task.id}
            title={task.title}
            description={task.description}
            handleDelete={() => {
              handleDelete(task.id);
            }}
            todoDeleteMsg={todoDeleteMsg}
            editTodo={() => {
              editTodo(task.id, task.title, task.description);
            }}
          />
      )} </div> );
        taskDatafilter =return( <div> {
           showTaskData.filter((value)=> value.completed === 1).map((task) =>
                <TodoItem
                    key={task.id}
                    title={task.title}
                    description={task.description}
                    handleDelete={() => {
                        handleDelete(task.id);
                    }}
                    todoDeleteMsg={todoDeleteMsg}
                    editTodo={() => {
                        editTodo(task.id, task.title, task.description);
                    }}
                />
        ))
        })
    }

all you have to do is to add ternary condition in your jsx like this你所要做的就是像这样在你的 jsx 中添加三元条件

In you constructor define you variable在你的构造函数中定义你的变量

 constructor(props){
      super(props)
      this.state = {
        showDataFilter: true,
      }

And in you jsx use this variable in ternary operator like this在你 jsx 中,像这样在三元运算符中使用这个变量

render(
  ...
  {(this.state.showDataFilter)?
     <taskDatafilter/>
    :
     <taskData/>
  }
)

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

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