Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing argument to reducer?

I have an action defined as such in the creator:

export const actionCreators = {
    submitlink: (url: string) => <SubmitLinkAction>{ type: 'SUBMIT_LINK' }
}

in the component calling it:

public render() {
    return <div>
        <div>{this.props.url}</div>
        <button onClick={() =>
        {
            this.props.submitlink("google.com");
        }}>submit</button>
    </div>
}

So I'm trying to figure out how in the reducer function I can grab thea rgument passed in? This is what the reducer looks like:

export const reducer: Reducer<SubmitLinkState> = (state: SubmitLinkState, action: KnownAction) => {...}

As an aside if this is not the correct workflow what should I do? The end goal is to have a text box and the submit buttonw ill send the contents of that text box to the reducer.

like image 499
meds Avatar asked Jun 04 '26 13:06

meds


1 Answers

You can pass the url as an action parameter like

export const actionCreators = {
    submitlink: (url: string) => <SubmitLinkAction>{ type: 'SUBMIT_LINK', url}
}

and then in the reducer access it as action.url

export const reducer: Reducer<SubmitLinkState> = (state: SubmitLinkState, action: KnownAction) => {
    ...
    console.log(action.url);
}
like image 58
Shubham Khatri Avatar answered Jun 07 '26 12:06

Shubham Khatri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!