【问题标题】:onClick change state of child component ReactJSonClick 改变子组件 ReactJS 的状态
【发布时间】:2019-12-02 01:34:34
【问题描述】:

我想更新两个组件之间的状态,我的反应知识有点生疏,但这就是我构建组件的方式。

子组件 - 向 API 端点发出 get 请求,并将数据呈现到我的图表库

    export default class EmployerLearningNeeds extends Component {
      constructor(props) {
        super(props)
        this.state = {
          employerData: [],
        }
      }

    componentDidMount() {
        this.fetchData() // 
      }

      fetchData = () => {
        axios.get(fullAPI).then(res => {
          const apiResponse = res.data
          apiResponse.map(employer => {
            console.log('ChildResponse', apiResponse)
            // eslint-disable-next-line no-param-reassign
            employer.value = employer.value.toFixed(2)
            return employer
          })
          this.setState({
            employerData: apiResponse,
          })
        })

  }

    getOption = () => ({ data: this.state.employerData })

    render() {
     return( 
    <ReactEcharts option={this.getOption()}
    )}

父组件 - 渲染我的子组件,按钮点击应该更新我的子组件的状态

export default class InterviewInsights extends Component {
  constructor(props) {
    super(props)
    this.state = {
      employerData: [],        }
  }

testFunction = () => {
    axios.get(apiURL).then(res => {
      console.log('Parent response', res.data)
      this.setState({
        employerData: res.data,
      })
    })
  }
render() {
    return (
            <button href="#" onClick={this.testFunction}>
              click me
            </button>
            <EmployerLearningNeeds employerData={this.state.employerData} />
    )
  }

【问题讨论】:

    标签: javascript reactjs ecmascript-6


    【解决方案1】:

    您从父组件传递到子组件的任何内容都将位于this.props,而不是this.state。

    从您的代码看来,您希望父级能够获取一组不同的数据并将其传递给子级进行渲染。

    如果父母要进行提取并将数据向下传递,为什么还要在孩子中使用fetchData? (如果我遗漏了什么,请道歉 - 不是 100% 确定您要做什么。)

    (因为 componentWillReceiveProps 现在已被替换为 static getDerivedStateFromProps)

    static getDerivedStateFromProps(props) {
        const apiResponse = props.employerData;
        apiResponse.map(employer => {
            console.log('ChildResponse', apiResponse);
    
            // eslint-disable-next-line no-param-reassign
            employer.value = employer.value.toFixed(2);
    
            return employer;
        })
    
        return { employerData: apiResponse };
    }
    

    每当父级为employerDetails 传入一组新数据、处理数据并将结果放入this.state 时都会触发(这意味着您可以保持render() 函数原样)。


    您也可以去掉 componentWillMount、fetchData 和 getOption,并将您的 render 更改为以下内容:

    render() {
        const { employerData } = this.props;
        const option = employerData.map(e => e.value = e.value.toFixed(2));
    
        return( 
            <ReactEcharts option={{ data: option }}
        )
    }
    

    【讨论】:

    • 抱歉,我发现这有点难以理解...如果我在呈现页面时从子组件中删除 componentDidMount,我的图表将为空,这不是我想要的,目的子组件是绘制图形并渲染通用数据。我的 Parent 组件中的 fetchData 背后的想法是,最终它将用作过滤组件,获取数据并根据需要更新状态。
    • 嗯好的,明白了。在这种情况下,您需要通过props 访问传递下来的数据,或者在父级传递新数据时使用getDerivedStateFromProps 更新子级的state。
    猜你喜欢
    • 2021-06-08
    • 2018-07-08
    • 2020-06-06
    • 2015-04-15
    • 2017-07-10
    • 2019-11-27
    • 2016-02-21
    • 2018-10-05
    • 2017-10-09
    相关资源
    最近更新 更多