【问题标题】:Sending parameter with function to child component and back to parent component将带有函数的参数发送给子组件并返回给父组件
【发布时间】:2019-07-27 12:15:34
【问题描述】:

我有一个名为 InputWithButton 的自定义组件,如下所示:

const InputWithButton = ({ type = "text", id, label, isOptional, name, placeholder = "", value = "", showPasswordReset, error, isDisabled, buttonLabel, handleChange, handleBlur, handleClick }) => (
    <StyledInput>
        {label && <label htmlFor="id">{label}{isOptional && <span className="optional">optioneel</span>}</label>}
        <div>
            <input className={error ? 'error' : ''} type={type} id={id} name={name} value={value} placeholder={placeholder} disabled={isDisabled} onChange={handleChange} onBlur={handleBlur} autoComplete="off" autoCorrect="off" />
            <Button type="button" label={buttonLabel} isDisabled={isDisabled} handleClick={() => handleClick(value)} />
        </div>
        {error && <Error>{Parser(error)}</Error>}
    </StyledInput>
);

export default InputWithButton;

Button 是另一个组件,如下所示:

const Button = ({ type = "button", label, isLoading, isDisabled, style, handleClick }) => (
    <StyledButton type={type} disabled={isDisabled} style={style} onClick={handleClick}>{label}</StyledButton>
);

export default Button;

我在这样的父组件中使用 InputWithButton 组件:

render() {
    const { name } = this.state;
    return (
        <React.Fragment>
            <InputWithButton label="Name" name="Name" buttonLabel="Search" value={name} handleChange={this.handleChange} handleClick={this.searchForName} />
        </React.Fragment>
    );
}

如果点击按钮,则调用searchForName函数:

searchForName = value => {
    console.log(value); //Input field value
}

这是可行的,但我想向它添加另一个参数,但这次是来自父组件的参数

// handleClick={() => this.searchForName('person')}

<InputWithButton label="Name" name="Name" buttonLabel="Search" value={name} handleChange={this.handleChange} handleClick={() => this.searchForName('person')} />

searchForName 中的输出现在是“人”而不是值。

我想我可以用以下代码解决这个问题:

searchForName = type => value => {
    console.log(type); //Should be person
    console.log(value); //Should be the value of the input field
}

但是这种方法不再执行该功能。

我该如何解决这个问题?

编辑:Codepen

【问题讨论】:

  • this.searchForName('person')} 只是调用带有 'person' 参数的函数 searchForName - 该函数只是将这个参数记录下来。您期望的价值是多少?
  • @DaleKing 我期待我从父组件传递的值是“人”,而我在 InputWithButton 中传递的值是“输入值”字段.
  • 啊,我明白你的意思了。你能抛出一个包含相关部分的代码沙箱吗?
  • @DaleKing 我添加了一个 Codepen 链接。 codepen.io/anon/pen/YgpOGE

标签: javascript reactjs ecmascript-6 arrow-functions


【解决方案1】:

正如我所怀疑的,只需传递一个对象并确保您接受了 handleClick 函数中的参数

handleClick={value =&gt; this.searchName({value, person: 'person'})}

或更详细 - 没有语法糖

handleClick={value =&gt; this.searchName({value: value, person: 'person'})}

然后你可以用 value.person 来解决它

完整的codepen here

希望对你有帮助

【讨论】:

    【解决方案2】:

    我会尝试handleClick={this.searchForName.bind(this, 'person')},如果它适合你,请告诉我。

    编辑: 我从你的 codepen 更改了片段,它正在工作:

    searchName(key, value) {
        console.log(key);
        console.log(value);
      }
    
      render() {
        const { name } = this.state;
    
        return (
          <InputWithButton name="name" value={name} buttonLabel="Search" handleChange={this.handleChange} handleClick={this.searchName.bind(this, 'person')} />
        )
      }
    

    【讨论】:

    • 在 searchForName 函数中 console.log 时这个参数的值是多少?你的 searchForName 应该是:searchForName(param1, param2) { //func body},没有额外的绑定 () =>
    • 编辑:这个参数是指从父组件传递的参数,在这种情况下是param2的控制台日志。
    • 它在没有额外绑定和 .bind 的情况下工作你能解释一下你的解决方案吗?
    • 文档 (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…) 对这个很有帮助:bind() 方法创建一个新函数,在调用该函数时,将其 this 关键字设置为提供的值,并具有给定的序列调用新函数时提供的任何参数之前的参数。
    猜你喜欢
    • 2019-05-30
    • 2018-11-30
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    • 2017-10-10
    • 1970-01-01
    相关资源
    最近更新 更多