0

I have a function like this:

let ChoosePage = (i) => {
let urls = [
    'http://localhost:3005/products/774944', 
    'http://localhost:3005/products/774945', 
    'http://localhost:3005/products/774946',
    'http://localhost:3005/products/123581', 
    'http://localhost:3005/products/782691', 
    'http://localhost:3005/products/782485',
    'http://localhost:3005/products/782486', 
    'http://localhost:3005/products/782487', 
    'http://localhost:3005/products/782488',
    'http://localhost:3005/products/738471'];
let urls_sliced = urls;
if (i === 0) {
     urls_sliced = urls.slice(0, 3);
 } else if (i === 1) {
     urls_sliced = urls.slice(4, 7);
 } else if (i === 2) {
     urls_sliced = urls.slice(8, 9);
 }
 let show_items = () => {
    ReactDOM.render(urls_sliced.map((url)=>{
            return(
              <Item url={url}/>
            )
            }), document.getElementById('items'));
        }

        show_items()

}
export default ChoosePage;

Im trying to call it by using onClick event in another file.js, like this:

import ChoosePage from './index';

<Page onClick={ChoosePage(0)}>1</Page>
<Page onClick={ChoosePage(1)}>2</Page>
<Page onClick={ChoosePage(2)}>3</Page>

It returns error that Object is not a function, what am I missing?

3
  • Try returning a function when ChoosePage is called. let ChoosePage = (i) => { return () => {...};}; export default ChoosePage; Commented Jun 11, 2019 at 11:12
  • try changing onClick={ChoosePage(0)} to onClick={()=>ChoosePage(0)} Commented Jun 11, 2019 at 11:12
  • don't export default, just add export to let ChoosePage and import it as : import { ChoosePage } from './index'; Commented Jun 11, 2019 at 11:16

2 Answers 2

1

You need this change:

import ChoosePage from './index';

<Page onClick={e=>{ChoosePage(0)}}>1</Page>
<Page onClick={e=>{ChoosePage(1)}}>2</Page>
<Page onClick={e=>{ChoosePage(2)}}>3</Page>
Sign up to request clarification or add additional context in comments.

Comments

0

Probably you must use a call back inside the onClick and looking like onClick={()=>{ChoosePage(0)}} or just pass function itself in the onClick body and see what happens onClick={ChoosePage}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.