简体   繁体   English

ReactJS两个不同的组件

[英]ReactJS two different components

I have a question regarding ReactJS components. 我对ReactJS组件有疑问。 Please bear with me as I'm just new in ReactJS. 因为我是ReactJS的新手,请多多包涵。 I'm stuck figuring out how to do this with ReactJS. 我一直在弄清楚如何使用ReactJS做到这一点。 Here's the scenario. 这是场景。

SCENARIO: 场景:

<div>
    <button id="download-button">Download as CSV</button> <!-- This is a ReactJS Component -->
    <h3>Filters</h3>
    <form>
        <!--
            SOME FILTER FIELDS
        -->
        <input type="submit" value="Filter" />
    </form>
</div>

<div>
    <h2>Filtered Search Result</h3>
    <div id="result" filters="<?php echo $filters ?>"></div> <!-- This is a ReactJS Component -->
</div>

PROBLEM: 问题:

Inside result component, if there's an empty result, I want to hide the download button. 在结果组件内部,如果结果为空,我想隐藏下载按钮。 I understand that this is simple if download button is inside result component. 我知道,如果下载按钮位于结果组件内部,则这很简单。 But in this case, I just don't want to write all the html elements that is irrelevant inside the result component just to be able to access result button on top of those html elements. 但是在这种情况下,我只是不想编写所有与结果组件无关的html元素,只是为了能够访问这些html元素顶部的结果按钮。

In react your components can return null in render() method 反应中,您的组件可以在render()方法中返回null

So this is valid 所以这是有效的

// @flow
import React from "react";
Props = {
    renderNull: boolean
}
const MyNullDecoratorComponent = (props: Props) => props.renderNull && null || "some text";

You can put both the Child components into one Parent component(having method checkResultLength and this.state.downloadVisible = true ). 您可以将两个Child组件都放入一个Parent组件中(具有checkResultLength方法和this.state.downloadVisible = true)。

Pass these two as props in result Component. 将这两个作为道具传递给结果Component。

Check in your result component in "componentDidMount" and call the function checkResultLength passed from the parent component which will set the state.downloadVisible = false if your results.length === 0. 在“ componentDidMount”中检入结果组件,并调用从父组件传递的函数checkResultLength,该函数将设置state.downloadVisible = false(如果您的results.length === 0)。

this.state.downloadVisible will be passed as a prop in your Filter Form component and you can hide and show it accordingly this.state.downloadVisible将作为prop传递到您的Filter Form组件中,您可以相应地隐藏和显示它

First of all, you should know how to communicate between two component. 首先,您应该知道如何在两个组件之间进行通信。 If this two component is parent <-> child, they can communicate by Props or function callback. 如果这两个组件是父<->子代,则它们可以通过Props或函数回调进行通信。

Your components are not parent <-> child relationship, so the best way is using flux data flow, You can treat your filter result as global component state. 您的组件不是父<->子关系,所以最好的方法是使用流量数据流。您可以将过滤器结果视为全局组件状态。

I think you should learn Flux data flow, such as Redux, Alt.js, etc, it can resolve your problem. 我认为您应该学习Flux数据流,例如Redux,Alt.js等,它可以解决您的问题。

I use Alt.js, Here is my demo here , I hope it will help you. 我用Alt.js,这里是我的演示在这里 ,我希望它会帮助你。

Since you are passing the result props to the Result you can just easily add a guard operator to your DownloadBtn Component 既然你是路过的result道具的Result ,你可以很容易地只保护操作者添加到您的DownloadBtn组件

 class Result extends Component { render () { const { result } = this.props // is the same with const result = this.props.result return ( <div> {result.length >= 0 && <DownloadBtn /> } { // render results here } </div> ) } } 

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

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