React's method of realizing secondary linkage

React's method of realizing secondary linkage

This article shares the specific code of React to achieve secondary linkage for your reference. The specific content is as follows

Implementation effect: Ordinary h5 page. I cropped the picture and deleted the user part, but it does not affect the description.

The general idea is to pass the data interface from the page to the component. After the interaction is executed in the component, the selected data result is returned to the page through onTimeChange and then displayed on the page.
I wrote it in Taro, the syntax is the same as react.

Mini Program Effect

A method from a long time ago, I will give you the implementation code:

1. There is a pop-up window module for selecting time on the page

 {this.state.isToggleOn && (
    <Panel
        onTimeChange={this.onTimeChange}
        onClick={this.closeMask}
        list={this.state.timeList} //Interface data status={this.state.status} //Current product status, optional/>
   )}

2. In the pop-up window

import { Component } from "@tarojs/taro";
import { View, Text } from "@tarojs/components";
import SendTime from "../time";
import "./index.scss";

export default class Panel extends Component {
  constructor(props) {
    super(props);
  }

  static defaultProps = {
    list: [],
    status: ""
  };

  onClick() {
    this.props.onClick();
  }

  onTimeChange(date, time) {
    this.props.onTimeChange(date, time);
  }

  render() {
    return (
      <View className="panel-modal">
        <SendTime
          list={this.props.list}
          onClick={this.onClick}
          onTimeChange={this.onTimeChange}
          status={this.props.status}
        />
      </View>
    );
  }
}

3. In the time component

import { Component } from "@tarojs/taro";
import { View, Text } from "@tarojs/components";
import { imageList } from "../../image";
import "./index.scss";

let dateNum = 0,
  timeNum = 0;
export default class SendTime extends Component {
  constructor(props) {
    super(props);

    this.state = {
      dateNum: dateNum,
      timeNum: timeNum,
      timeList: [],
    };
  }

  static defaultProps = {
    list: [],
  };

  onClick() {
    this.props.onClick();
  }

  switchDay(index, info) {
    this.setState({
      dateNum: index,
    });
    dateNum = index;
    this.switchTime(timeNum);
    let date = info ? info.date : "";
    let time = info && info.timeSegementList ? info.timeSegementList[0] : "";
    this.setState({
      timeList: info.timeSegementList,
    });
    this.onTimeChange(date, time);
  }

  switchTime(index) {
    let dateNum = this.state.dateNum;
    this.setState({
      timeNum: index,
    });
    timeNum = index;
    let date = this.props.list[dateNum] ? this.props.list[dateNum].date : "";
    let time = this.props.list[dateNum]
      ? this.props.list[dateNum].timeSegementList[index]
      : "";
    if (index != 0) {
      this.onTimeChange(date, time);
    }
  }

  onTimeChange(date, time) {
    this.props.onTimeChange(date, time);
  }

  componentWillMount() {
    this.setState({
      timeList: ["Ship immediately after group is formed"],
    });
  }

  componentDidMount() {
    if (this.props.status) {
      this.switchDay(dateNum, this.props.list[dateNum]);
    } else {
      (dateNum = 0), (timeNum = 0);
      this.setState(
        {
          dataNum: 0,
          timeNum: 0,
        },
        () => {
          this.switchDay(0, this.props.list[0]);
        }
      );
    }
  }

  getClassName(index) {
    switch (index) {
      case this.state.dateNum:
        return "send-data-li current";
      default:
        return "send-data-li";
    }
  }

  render() {
    return (
      <View>
        <View className="send-time-title" onClick={this.onClick}>
          <Text>Delivery time</Text>
          <View className="close" />
        </View>
        <View className="send-time-cont">
          <View className="send-date-list">
            {this.props.list.map((info, index) => (
              <View
                key={index}
                className={
                  index === this.state.dateNum
                    ? "send-data-li current"
                    : "send-data-li"
                }
                onClick={this.switchDay.bind(this, index, info)}
              >
                <Text className="txt">{info ? info.date : ""}</Text>
              </View>
            ))}
          </View>
          <View className="send-r-time">
            {this.state.timeList.map((info, index) => (
              <View
                key={index}
                className={
                  index === this.state.timeNum
                    ? "send-r-li current"
                    : "send-r-li"
                }
                onClick={this.switchTime.bind(this, index)}
              >
                <View class="send-r-flex">
                  <Text class="txt">{info}</Text>
                  <Image
                    className="blue-ok"
                    src={imageList.blueOk}
                    mode={"aspectFit"}
                    lazy-load={true}
                  />
                </View>
              </View>
            ))}
          </View>
        </View>
      </View>
    );
  }
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • React realizes secondary linkage (left and right linkage)
  • React realizes secondary linkage effect (staircase effect)
  • React+ts realizes secondary linkage effect

<<:  Detailed explanation of MySQL monitoring tool mysql-monitor

>>:  Detailed explanation of Linux rpm and yum commands and usage

Recommend

Keepalived implements Nginx load balancing and high availability sample code

Chapter 1: Introduction to keepalived The purpose...

Sample code for implementing PC resolution adaptation in Vue

Table of contents plan Install Dependencies Intro...

About installing python3.8 image in docker

Docker Hub official website 1. Search for Python ...

Correct way to load fonts in Vue.js

Table of contents Declare fonts with font-face co...

Solution for Docker Swarm external verification load balancing not taking effect

Problem Description I created three virtual machi...

Weather icon animation effect implemented by CSS3

Achieve results Implementation Code html <div ...

Analysis of multi-threaded programming examples under Linux

1 Introduction Thread technology was proposed as ...

JavaScript implementation of carousel example

This article shares the specific code for JavaScr...

Detailed explanation of how to use eslint in vue

Table of contents 1. Description 2. Download rela...

How to configure Http, Https, WS, and WSS in Nginx

Written in front In today's Internet field, N...

Angular framework detailed explanation of view abstract definition

Preface As a front-end framework designed "f...

Use and analysis of Mysql Explain command

The mysql explain command is used to show how MyS...

Detailed Analysis of Event Bubbling Mechanism in JavaScript

What is bubbling? There are three stages in DOM e...

JavaScript two pictures to understand the prototype chain

Table of contents 1. Prototype Relationship 2. Pr...