简体   繁体   English

如何使用React Redux在const click函数中调用函数?

[英]How to call a function inside the const click function using React Redux?

I'm new with react and redux framework and I need help on how to invoke or call another function from within. 我是React和Redux框架的新手,我需要有关如何从内部调用或调用另一个函数的帮助。 As you can see my code below from ToggleSwitch.jsx file. 如您所见,我的代码来自ToggleSwitch.jsx文件。 I have a click function and I want to call a toaster function which displays a toast alert but it's not being invoked so I check on the console and it has an error log something like " Reference Error: toaster is not defined ". 我有一个单击功能,我想调用一个烤面包机功能,该功能显示一个烤面包警报,但没有被调用,因此我在控制台上进行了检查,它有一个类似“ 参考错误:未定义烤面包机 ”的错误日志。 What did I miss here? 我在这里想念什么? Please guide me on how to resolve this type of scenario. 请指导我如何解决这种情况。 Thanks. 谢谢。

ToggleSwitch.jsx ToggleSwitch.jsx

const ToggleSwitch = ({ className, input, label, meta: { touched, error }, ...rest }) => (
    <div>
        <Switch input={{ defaultChecked: input.value }}
            className={className}
            onChange={e => input.onChange(e.target.checked)}
            onClick={this.onCheckboxReturningLaterClick()}
            label={label}
            {...rest} />
        <input name={input.name} value={input.value} type="hidden" />
    </div>

)

const onCheckboxReturningLaterClick = () => {

      toastr.error(dude.messages.UNABLE_TO_PROCEED) 

}

The onCheckboxReturningLaterClick function is not in this of the ToggleSwitch component. onCheckboxReturningLaterClick功能在this的的ToggleSwitch组件。 The syntax you have used for the component: 您用于组件的语法:

const Component = (props) => <h1>props.title</h1>

Is a stateless type of component. 是无状态类型的组件。

Try to remove the this keyword when you're invoking onCheckboxReturningLaterClick in the onClick handler. 当您在onClick处理程序中调用onCheckboxReturningLaterClick时,请尝试删除this关键字。

Another problem could be that... toastr is really not defined anywhere in the scope :) 另一个问题可能是...范围内的任何地方实际上都没有定义toastr :)

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

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