简体   繁体   English

反应:仅当条件为真时才呈现子项

[英]React: Render children only if condition is true

I'm new in React.我是 React 的新手。 I'm developing a screen but I have a issue, I don't know how insert the children in the parent if the state condition is equals, I'm using an array to print the parent and children but depends of the data the parent could have a children or not, for example if (parent.rework_name === children.rework_name) ?我正在开发一个屏幕,但我有一个问题,如果状态条件相等,我不知道如何在父级中插入子级,我正在使用数组来打印父级和子级,但取决于父级的数据是否可以有孩子,例如 if (parent.rework_name === children.rework_name) ? print the children : 'nothing in the parent'.打印孩子:'父母没有任何东西'。

Please let me know if you have an idea how to solve this, many many thanks in advance.如果您知道如何解决这个问题,请告诉我,非常感谢。

This is the goal, my code works but the damn children is outside the parent :(这是目标,我的代码有效,但该死的孩子在父母之外:(

在此处输入图片说明

    class Filling extends Component {

  constructor() {
    super();

    this.state = {
      fillingStations: [],
    };
  }

  componentDidMount() {

    getDataAPI('http://localhost:8080/api/parent')
      .then((station) => {

        getDataAPI('http://localhost:8080/api/children')
          .then((data) => {

            const stationArray = [];

            station.map((item, index) => {

              stationArray.push(
                <ReworkStation key={index} title={index + 1} status='' />,
              );

              data.map((it, idx) => {

                const f2Date = it.f2_time.substr(0, 10);
                const f2Hour = it.f2_time.substr(11, 8);
                const f2DateFormatted = `${f2Date.substr(8, 2)}/${f2Date.substr(5, 2)}/${f2Date.substr(0, 4)}`;
                const color = selection_color(it.color_d);

                return (
                  stationArray.push(item.rework_name === it.rework_name && <ReworkTitle key={idx} vin={it.vin} date={f2DateFormatted} ipsq={it.defects} hour={f2Hour} color={color} />)
                );
              });

            });

            console.log(stationArray);

            this.setState({
              fillingStations: stationArray,
            });

          });

      });

  }

  render() {

    return (
      <div className='row'>
        { this.state.fillingStations }
      </div>
    );
  }
}

I don't know how to insert the children inside the parent already render.我不知道如何在已经渲染的父级中插入子级。

在此处输入图片说明

I already solved, first render all the parent divs and after replace the position array with array.splice我已经解决了,首先渲染所有父div,然后用array.splice替换位置数组

render() {

const array = [];
this.state.fillingStations.map((item, index) => (

  array.push(<Parent key={index} title={index + 1} status='' />),

  this.state.fillingChildren.map((it, ind) => {

    if (item.name === it.name) {

      parent.splice(index, 1,
        <Parent {...this.props}}>
          <Child {...this.props} />
        </Parent >);
    }
  })

));

return (
  <div className='row'>
    {array}
  </div>
);

} } } }

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

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