Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use Splitting in React JS #61

Open
ghost opened this issue Jul 28, 2020 · 7 comments
Open

Unable to use Splitting in React JS #61

ghost opened this issue Jul 28, 2020 · 7 comments

Comments

@ghost
Copy link

ghost commented Jul 28, 2020

for some reason the code just doesn't execute, no warnings no errors, it just doesn't run
I have tried both the CDN and NPM methods, both fail. What is the problem here?

To recreate my scenario, just create a react project using npx create-react-app and then try adding splitting js either through NPM or CDN

@lukethacoder
Copy link

lukethacoder commented Aug 10, 2020

Not too sure what you've attempted but the below worked for me using NPM.

import React, { useState, useRef, useEffect } from 'react'

import 'splitting/dist/splitting.css'
import 'splitting/dist/splitting-cells.css'
import Splitting from 'splitting'


export const SplittingJS = () => {
  const [lines, setLines] = useState([])
  const splitRef = useRef(null)

  useEffect(() => {
    if (splitRef) {
      let split_res = Splitting({
        by: 'lines',
      })
      setLines(split_res[0].lines)
      console.log(`Split text into ${lines} lines`)
    }
  }, [splitRef])

  return (
    <p
      ref={splitRef}
      data-splitting='true'
    >
      split some text here
    </p>
  )
}

@Reyyah09
Copy link

Kindly explain this part of your code:
const [lines, setLines] = useState([])
const splitRef = useRef(null)

useEffect(() => {
if (splitRef) {
let split_res = Splitting({
by: 'lines',
})
setLines(split_res[0].lines)
console.log(Split text into ${lines} lines)
}
}, [splitRef])

@lukethacoder
Copy link

@Reyyah09 I should have commented my code better, my bad.

import React, { useState, useRef, useEffect } from 'react'

import 'splitting/dist/splitting.css'
import 'splitting/dist/splitting-cells.css'
import Splitting from 'splitting'


export const SplittingJS = () => {
  // state for the array of lines found after running Splitting
  const [lines, setLines] = useState([])
  // create a ref object to our text to split
  const splitRef = useRef(null)

  // should fire anytime splitRef is changed (onload splitRef won't exist)
  useEffect(() => {
    // double checking we actually have a reference (and the value is not null)
    if (splitRef) {
      // run the SplittingJS magic here, using 'lines' as the splitting technique
      let split_res = Splitting({
        by: 'lines',
      })
      // finding the first block of text and its lines - then assigning it to our state defined above
      setLines(split_res[0].lines)
      console.log(`Split text into ${lines} lines`)
    }
  }, [splitRef])

  return (
    <p
      ref={splitRef}
      data-splitting='true'
    >
      split some text here
    </p>
  )
}

@ghost
Copy link
Author

ghost commented Aug 19, 2020

@lukethacoder I think this method will work, I haven't tried this yet

@jackdewhurst
Copy link

@lukethacoder this was very helpful thank you!

@jackdewhurst
Copy link

jackdewhurst commented Apr 28, 2021

@lukethacoder do you know how to re-split if I were to change the text split some text here programmatically, say via a timeout?

@lukethacoder
Copy link

@jackdewhurst suppose something like this could work (havnt tested this - but makes sense in my head)

import React, { useState, useRef, useEffect } from 'react'

import 'splitting/dist/splitting.css'
import 'splitting/dist/splitting-cells.css'
import Splitting from 'splitting'

export const SplittingJS = () => {
  // state for the array of lines found after running Splitting
  const [lines, setLines] = useState([])
  // create a ref object to our text to split
  const splitRef = useRef(null)

  // should fire anytime splitRef is changed (onload splitRef won't exist)
  useEffect(() => {
    splitTheWords()
  }, [splitRef])

  const splitTheWords = () => {
    // double checking we actually have a reference (and the value is not null)
    if (splitRef) {
      // run the SplittingJS magic here, using 'lines' as the splitting technique
      let split_res = Splitting({
        by: 'lines',
      })
      // finding the first block of text and its lines - then assigning it to our state defined above
      setLines(split_res[0].lines)
      console.log(`Split text into ${lines} lines`)
    }
  }

  const testFireOffSplit = () => {
    // this could be run via setTimeout instead
    splitTheWords()
  }

  return (
    <>
      <p ref={splitRef} data-splitting='true'>
        split some text here
      </p>
      <button onClick={testFireOffSplit}>re-split</button>
    </>
  )
}

or could run it from another useEffect that is watching another value change too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants