question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use gradient colors in RN projects

See original GitHub issue

I looked at the examples for the web version, but it didn’t solve my problem.

I run react-native init myapp to create a RN project, and run yarn add react-native-svg react-native-countdown-circle-timer@3.0.8 to add dependencies.

This is my App.js

import React from 'react';
import {Text, View} from 'react-native';
import Svg, {Defs, LinearGradient, Stop} from "react-native-svg";
import { CountdownCircleTimer } from 'react-native-countdown-circle-timer'

export default function App() {
  return (
    <View>
      <Svg width="0" height="0" >
        <Defs>
          <LinearGradient id="your-unique-id" x1="1" y1="0" x2="0" y2="0">
            <Stop offset="5%" stopColor="gold"/>
            <Stop offset="95%" stopColor="red"/>
          </LinearGradient>
        </Defs>
      </Svg>
      <CountdownCircleTimer
        colors="url(#your-unique-id)"
        size={200}
        strokeWidth={20}
        duration={60}
        initialRemainingTime={25}>
        {({remainingTime}) => <Text>{remainingTime}</Text>}
      </CountdownCircleTimer>
    </View>
  );
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
vydimitrovcommented, Apr 13, 2022

Hey you can restart the timer the same way you do it with the component, by passing a key. So below is your CountdownCircleTimer:

// src/components/timer - this is your folder for shared components
export const CountdownCircleTimer = ({ duration, isPlaying, .... }) => {
  const {
    path,
    pathLength,
    stroke,
    strokeDashoffset,
    remainingTime,
    elapsedTime,
    size,
    strokeWidth,
  } = useCountdown({ isPlaying: true, duration, colors: 'url(#your-unique-id)' })

  return (
    <View style={styles.container}>
      <View style={{ width: size, height: size, position: 'relative' }}>
        <Svg width={size} height={size}>
          <Defs>
            <LinearGradient id="your-unique-id" x1="1" y1="0" x2="0" y2="0">
              <Stop offset="5%" stopColor="gold"/>
              <Stop offset="95%" stopColor="red"/>
            </LinearGradient>
          </Defs>
          <Path
            d={path}
            fill="none"
            stroke="#d9d9d9"
            strokeWidth={strokeWidth}
          />
          {elapsedTime !== duration && (
            <Path
              d={path}
              fill="none"
              stroke={stroke}
              strokeLinecap="butt"
              strokeWidth={strokeWidth}
              strokeDasharray={pathLength}
              strokeDashoffset={strokeDashoffset}
            />
          )}
        </Svg>
        <View style={styles.time}>
          <Text style={{ fontSize: 36 }}>{remainingTime}</Text>
        </View>
      </View>
    </View>
  );
}

Then in your App you just use it as it was before

import { CountdownCircleTimer } from 'src/components/timer '

<CountdownCircleTimer key="some-new-key-to-restart" duration={10} isPlaying />

Does it help?

0reactions
anastasiapyzhikcommented, Apr 14, 2022

Worked for me as well. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating complex gradients with react-native-linear-gradient
A gradient is a design technique that blends more than one color together in a smooth transition. For example, think of the Instagram...
Read more >
React Native Linear Gradient - GitHub
An array of at least two color values that represent gradient colors. Example: ['red', 'blue'] sets gradient from red to blue. start. An...
Read more >
Does React Native styles support gradients? - Stack Overflow
Just export your gradient as SVG and use it using react-native-svg and when after you import your component set width and height and ......
Read more >
7 Ways to Use Gradient Colors in Your Creative Projects
From gradient backgrounds to social media templates, here's how to use gradient colors in your creative projects.
Read more >
rn-gradients - npm
Start using rn-gradients in your project by running `npm i rn-gradients`. There are no other projects in the npm registry using rn-gradients.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found