TypeError: Cannot set property '$' of undefined

TypeError: Cannot set property '$' of undefined

RookieeeeeRookieeeee Posts: 6Questions: 1Answers: 0

hi,i want use datatables in react. use
const $ = require('jquery');
dt(window, $);
but have a error
TypeError: Cannot set property '$' of undefined
i need help who can help me
thanks very much

Answers

  • colincolin Posts: 15,143Questions: 1Answers: 2,586

    Hi @Rookieeeee ,

    Try

    var $  = require( 'jquery' );
    var dt = require( 'datatables.net' )( window, $ );
    

    Cheers,

    Colin

  • colincolin Posts: 15,143Questions: 1Answers: 2,586
  • RookieeeeeRookieeeee Posts: 6Questions: 1Answers: 0

    hi @colin i use

    var $ = require( 'jquery' );
    var dt = require( 'datatables.net' )( window, $ );

    but alwyas have the questions

  • colincolin Posts: 15,143Questions: 1Answers: 2,586

    Hi @Rookieeeee ,

    Could you link to your page, or paste your code here, please.

    Cheers,

    Colin

  • RookieeeeeRookieeeee Posts: 6Questions: 1Answers: 0

    Hi @colin


    thanks your help

  • RookieeeeeRookieeeee Posts: 6Questions: 1Answers: 0

    i use
    var $ = require( 'jquery' );
    var dt = require( 'datatables.net' )( window, $ );
    always have this question

    i create project use create-react-app

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin

    Can you create an example repo showing the issue for me so I can have a little play around with it?

    Thanks,
    Allan

  • RookieeeeeRookieeeee Posts: 6Questions: 1Answers: 0

    HI @allan my pro demo :https://github.com/xuningyang/datatables.git

    you can use: git clone https://github.com/xuningyang/datatables.git

    and npm install and npm start

    Thanks

  • RookieeeeeRookieeeee Posts: 6Questions: 1Answers: 0

    hi @allan @colin
    you can use demo to show problem;

    Thanks

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin

    Thanks! I removed your two require lines for DataTables and jQuery and replaced with the following at the top of the file:

    import $ from 'jquery';
    import dt from 'datatables.net';
    

    Checking that DataTables was loaded I used console.log( dt.version ); which did the job nicely (the console showed 1.10.19). Yo should be able to use jQuery and DataTables from there :).

    Allan

This discussion has been closed.