小编gra*_*unk的帖子

使用不同的 props 重用 React 组件的多个实例

所以我有一个子组件,我想在父容器组件中渲染它的多个实例。向每个对象传递不同的道具,以便它们显示不同。

发生的情况是,它们都在渲染时将脚本中的最后一个道具实例读入两个实例中。因此,下面的两个组件都以 placeHolder==='Describe myself' 结尾,是否有解决方法,以便它们分别轮流注入它们的 props?

           <ButtonMode 
              open={this.state.open}
              handleClose={this.handleClose}
              buttonName='Update'
              modalOpen={this.modalOpen}    
              placeHolder="New picture url"
              change={this.handlePicture}
              label='URL'
            />

           <ButtonMode 
              open={this.state.open}
              handleClose={this.handleClose}
              buttonName='Update'
              modalOpen={this.modalOpen}     
              placeHolder='Describe yourself'
              label='Bio'
              change={this.handleBio}
                />
Run Code Online (Sandbox Code Playgroud)

按钮模式

class ButtonMode extends Component {
    constructor(props){
        super(props)
        this.state = {
            input:''
        }
        this.handleInput = this.handleInput.bind(this);
        this.handle = this.handle.bind(this);
    }

    handleInput(val){
        this.setState({input:val})
    };

    handle() {

        this.props.change(this.state.input);
    };

    render(){
        const { classes } = this.props;
        return (
            <div>
                <Button 
                    className={classes.button}
                    onClick={this.props.modalOpen}
                    >Update
                </Button>
                <Modal
                    aria-labelledby="simple-modal-title"
                    aria-describedby="simple-modal-description"
                    open={this.props.open}
                    onClose={this.props.handleClose}
                    >
                    <div className={classes.paper}>
                        <TextField
                            id="filled-textarea"
                            label={this.props.label} …
Run Code Online (Sandbox Code Playgroud)

reusability reactjs react-props react-component

6
推荐指数
1
解决办法
8065
查看次数