点击时将道具传递给子组件(pass props to child component on click)

系统教程 行业动态 更新时间:2024-06-14 16:57:17
点击时将道具传递给子组件(pass props to child component on click)

我想在提交按钮的handlesubmitclick props传递给我的子组件(tabs)。 我是怎么做到的?

import React, { Component } from 'react' import Tabs from './tabs' import {connect} from 'react-redux' import {createParking, securedandparkingtype, numberofspaces, schedule} from '../../../store/actions/wizard' import {bindActionCreators} from 'redux' import Formsy from 'formsy-react' class PlaceContainer extends Component { handleSubmit(data) { const componentKey = this.props.children.props.location.pathname.split('/')[3] if (componentKey === 'location') { this.props.createParking(data) } else if (componentKey === 'secured') { this.props.securedandparkingtype(data) } else if (componentKey === 'spaces') { this.props.numberofspaces(data) } else if (componentKey === 'schedule') { this.props.schedule(data) } } render() { return ( <div className="row clearfix"> <div className="col-lg-12 col-md-12 col-sm-12 col-xs-12"> <div className="card"> <div className="header"> <h2>MANAGE PARKING PLACE</h2> </div> <div className="body"> <div className="row"> <Tabs/> <Formsy.Form onSubmit={this.handleSubmit.bind(this)}> {this.props.children} <input type="submit" className="btn btn-primary"/> </Formsy.Form> </div> </div> </div> </div> </div> ) } } export default PlaceContainer

I want to pass props to my child component (tabs) on handlesubmitclick of submit button. How i do that ?

import React, { Component } from 'react' import Tabs from './tabs' import {connect} from 'react-redux' import {createParking, securedandparkingtype, numberofspaces, schedule} from '../../../store/actions/wizard' import {bindActionCreators} from 'redux' import Formsy from 'formsy-react' class PlaceContainer extends Component { handleSubmit(data) { const componentKey = this.props.children.props.location.pathname.split('/')[3] if (componentKey === 'location') { this.props.createParking(data) } else if (componentKey === 'secured') { this.props.securedandparkingtype(data) } else if (componentKey === 'spaces') { this.props.numberofspaces(data) } else if (componentKey === 'schedule') { this.props.schedule(data) } } render() { return ( <div className="row clearfix"> <div className="col-lg-12 col-md-12 col-sm-12 col-xs-12"> <div className="card"> <div className="header"> <h2>MANAGE PARKING PLACE</h2> </div> <div className="body"> <div className="row"> <Tabs/> <Formsy.Form onSubmit={this.handleSubmit.bind(this)}> {this.props.children} <input type="submit" className="btn btn-primary"/> </Formsy.Form> </div> </div> </div> </div> </div> ) } } export default PlaceContainer

最满意答案

将状态this.state = {myProps: null}到组件中。 将你的状态tabs为道具: <Tabs {...this.state.myProps}

单击“提交”按钮时,使用所需的道具更新状态:

this.setState({myProps: this.props})

它将使用新道具更新Tabs 。

结果是:

class PlaceContainer extends Component { handleSubmit(data) { this.setState({myProps: this.props}) } constructor(){ this.state={{myProps: null}}; } render() { return ( ... <Tabs {...this.state.myProps}/> ... ); } } Add a state this.state = {myProps: null} to your component. Give tabs your state as props: <Tabs {...this.state.myProps}

When clicking on submit button, update the state with the wanted props:

this.setState({myProps: this.props})

it will update Tabs with new props.

As a result:

class PlaceContainer extends Component { handleSubmit(data) { this.setState({myProps: this.props}) } constructor(){ this.state={{myProps: null}}; } render() { return ( ... <Tabs {...this.state.myProps}/> ... ); } }

更多推荐

本文发布于:2023-04-12 20:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/41b1912cea7ac853e402f8e9526c61c5.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时将   道具   组件   pass   click

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!