简体   繁体   English

为什么我的React Component的Render Props方法不起作用?

[英]Why is my method Render Props of the React Component not working?

I have a problem. 我有个问题。 I'm trying do the method Render Prop but it not is working. 我正在尝试执行Render Prop方法,但是它不起作用。

My project is: It has to render some names of ComponentDidMount , and I can get it to do the filter and to filter the names. 我的项目是:它必须呈现ComponentDidMount一些名称,并且我可以获取它来进行过滤和名称过滤。 But I passed the function filter for a component, and do the Render Prop . 但是我为组件传递了函数filter ,并执行了Render Prop

I pass it here: 我在这里通过:

 import React from 'react'; import './Body.css'; import { Link } from "react-router-dom"; import axios from 'axios'; import Filter from './Filter'; class Body extends React.Component { constructor(props) { super(props); this.state = { employee: [] } } componentDidMount() { axios .get("http://127.0.0.1:3004/employee") .then(response => this.setState({ employee: response.data })); } getName = (filter) => { const { employee, add } = this.state; return employee.filter(employee => employee.name.includes(filter)).map(name => ( <div className='item' key={name.id}> <Link className="link" to={`/user/${name.id}`}> <div key={name.id}> <img className="img" alt="imgstatic" src={`https://picsum.photos/${name.id}`} /> </div> <h1 className="name2"> {name.name} </h1> </Link> </div> )); }; getValueInput = (evt) => { const inputValue = evt.target.value; this.setState({ input: inputValue }); } render() { return ( <div> <h4 className="manager"> Hello {this.props.currentManager}, here be all employees available for change. </h4> <div className="body"> {this.getName()} </div> <div className='input'> <Filter render={this.getName} /> </div> </div> ) } } export default Body; 

And here I get him: 在这里,我得到了他:

 import React from 'react'; class Filter extends React.Component { constructor() { super(); this.state = { input: '' } } getValueInput = (evt) => { const inputValue = evt.target.value; this.setState({ input: inputValue }); console.log(); console.log(this.state.input) } render() { return ( <div> <input placeholder='Search name here' type="text" onChange={this.getValueInput} /> </div> ) } } export default Filter 

But something's not working... 但是有些东西不起作用...

Can someone help me? 有人能帮我吗?

You are not at all using the render prop being supplied to the Filter component. 您根本没有使用提供给Filter组件的渲染道具。 Also the objective of render prop is to render the data, go using this.getName() inside the render Body Component isn't correct either(for one you are not passing the filter value to the getName). 同样,渲染道具的目的是渲染数据,在渲染主体组件中使用this.getName()也不正确(因为您没有将过滤器值传递给getName)。 You would use it like 你会用它像

import React from 'react';
import './Body.css';
import { Link } from "react-router-dom";
import axios from 'axios';
import Filter from './Filter';

class Body extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      employee: []
    }
  }

  componentDidMount() {
    axios
      .get("http://127.0.0.1:3004/employee")
      .then(response => this.setState({ employee: response.data }));
  }

  getName = (filter) => {
    const { employee, add } = this.state;
    return employee.filter(employee => employee.name.includes(filter)).map(name => (
      <div className='item' key={name.id}>
        <Link className="link" to={`/user/${name.id}`}>
          <div key={name.id}>
            <img className="img" alt="imgstatic"
              src={`https://picsum.photos/${name.id}`}
            />
          </div>
          <h1 className="name2"> {name.name} </h1>
        </Link>
      </div>
    ));
  };


  getValueInput = (evt) => {
    const inputValue = evt.target.value;
    this.setState({ input: inputValue });
  }

  render() {
    return (
      <div>
        <h4 className="manager"> Hello {this.props.currentManager}, here be all employees available for change. </h4>
        <div className='body'>
          <Filter render={this.getName} />
        </div>
      </div>
    )
  }
}

export default Body;

and Filter as 并过滤为

import React from 'react';

class Filter extends React.Component {
  constructor() {
    super();

    this.state = {
      input: ''
    }
  }

  getValueInput = (evt) => {
    const inputValue = evt.target.value;
    this.setState({ input: inputValue });
    console.log();

    console.log(this.state.input)
  }


  render() {
    return ( 
      <React.Fragment>
          {this.props.render(this.state.input)}
          <div className='input'>
            <input placeholder='Search name here' type="text" onChange={this.getValueInput} />
          </div>
      </React.Fragment>
    )
  }
}

Note React.Fragment is available from v16.2.0 onwards and if you are not using the relevant version replace React.Fragment with <div> 注意React.Fragment从v16.2.0开始可用,如果您不使用相关版本, React.Fragment<div>替换React.Fragment

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

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