How to use function inside component that return string on react/next.js?

Viewed 190

Below code works well when I test on local environment. But when I am going to build on netlify, it shows error.

const Fundraiser = (props) => {
    const truncate = ( str, n, useWordBoundary ) => {
        if (str.length <= n) { return str; }
        const subString = str.substr(0, n-1);
        return ((useWordBoundary?subString.substr(0, subString.lastIndexOf(" ")):subString) + "...");
    }
    return (
        ...
        <p className="m-b-0 font-14">{props.buttonShow?truncate(props.description, 350, true):props.description}</p>
        ...
    )

}
export default Fundraiser;

-netlify build log

7:41:48 AM:     at ReactDOMServerRenderer.read (/opt/build/repo/node_modules/react-dom/cjs/react-dom-server.node.development.js:3690:29)
7:41:48 AM:     at Object.renderToString (/opt/build/repo/node_modules/react-dom/cjs/react-dom-server.node.development.js:4298:27)
7:41:48 AM:     at Object.renderPage (/opt/build/repo/node_modules/next/dist/server/render.js:596:45)
7:41:48 AM:     at Function.getInitialProps (/opt/build/repo/.next/server/pages/_document.js:601:19)
7:41:48 AM:     at Object.loadGetInitialProps (/opt/build/repo/node_modules/next/dist/shared/lib/utils.js:69:29)
7:41:48 AM: info  - Generating static pages (13/13)
7:41:48 AM: > Build error occurred
7:41:48 AM: Error: Export encountered errors on following paths:
7:41:48 AM:     /explorer
7:41:48 AM:     at /opt/build/repo/node_modules/next/dist/export/index.js:487:19
7:41:48 AM:     at processTicksAndRejections (node:internal/process/task_queues:96:5)
7:41:48 AM:     at async Span.traceAsyncFn (/opt/build/repo/node_modules/next/dist/telemetry/trace/trace.js:60:20)
7:41:48 AM:     at async /opt/build/repo/node_modules/next/dist/build/index.js:833:17
7:41:48 AM:     at async Span.traceAsyncFn (/opt/build/repo/node_modules/next/dist/telemetry/trace/trace.js:60:20)
7:41:48 AM:     at async /opt/build/repo/node_modules/next/dist/build/index.js:707:13
7:41:48 AM:     at async Span.traceAsyncFn (/opt/build/repo/node_modules/next/dist/telemetry/trace/trace.js:60:20)
7:41:48 AM:     at async Object.build [as default] (/opt/build/repo/node_modules/next/dist/build/index.js:77:25)
7:41:48 AM: error Command failed with exit code 1. (https://ntl.fyi/exit-code-1)
7:41:48 AM: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
7:41:48 AM: ​
7:41:48 AM: ────────────────────────────────────────────────────────────────
7:41:48 AM:   "build.command" failed                                        
7:41:48 AM: ────────────────────────────────────────────────────────────────
7:41:48 AM: ​
7:41:48 AM:   Error message
7:41:48 AM:   Command failed with exit code 1: yarn build (https://ntl.fyi/exit-code-1)
7:41:48 AM: ​
7:41:48 AM:   Error location
7:41:48 AM:   In Build command from Netlify app:
7:41:48 AM:   yarn build
7:41:48 AM: ​

I newly defined truncate function to cut off the string. The script works well if I don't use truncate function on local and netlify as well. That tells the truncate function has a problem. Could you tell me what is wrong on my code?

0 Answers
Related