react-native-barcode-svg icon indicating copy to clipboard operation
react-native-barcode-svg copied to clipboard

Remove deprecated `defaultProps`

Open boldwade opened this issue 1 year ago โ€ข 1 comments

Hi! ๐Ÿ‘‹

Firstly, thanks for your work on this project! ๐Ÿ™‚

Today I used patch-package to patch [email protected] for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-barcode-svg/src/index.js b/node_modules/react-native-barcode-svg/src/index.js
index fd55c23..991acbd 100755
--- a/node_modules/react-native-barcode-svg/src/index.js
+++ b/node_modules/react-native-barcode-svg/src/index.js
@@ -7,6 +7,8 @@ import { linearizeEncodings, merge } from './shared';
 
 // This encode() handles the Encoder call and builds the binary string to be rendered
 const encode = (text, Encoder, options) => {
   // If text is not a non-empty string, throw error.
   if (typeof text !== 'string' || text.length === 0) {
     throw new Error('Barcode value must be a non-empty string');
@@ -103,9 +105,23 @@ const drawSvgBars = (encodings, options = {}) => {
 };
 
 export default function Barcode(props) {
+  const defaultProps = {
+    value: '',
+    format: 'CODE128',
+    singleBarWidth: 2, // width
+    maxWidth: undefined,
+    height: 100,
+    lineColor: '#000000',
+    backgroundColor: '#FFFFFF',
+    onError: undefined,
+  };
+
+  const mergedProps = merge(defaultProps, props);
+
   const {
     value, format, singleBarWidth, maxWidth, height, lineColor, backgroundColor, onError,
-  } = props;
+  } = mergedProps;
+
   const [bars, setBars] = useState([]);
   const [barcodeWidth, setBarCodeWidth] = useState(0);
   const [barcodeContainerWidth, setBarcodeContainerWidth] = useState(0);
@@ -123,10 +139,10 @@ export default function Barcode(props) {
   useEffect(() => {
     try {
       const encoder = barcodes[format];
-      const linearEncodings = encode(value, encoder, props);
+      const linearEncodings = encode(value, encoder, mergedProps);
 
       const barcodeTotalWidth = getTotalWidthOfEncodings(linearEncodings) * singleBarWidth;
-      const theBars = drawSvgBars(linearEncodings, props);
+      const theBars = drawSvgBars(linearEncodings, mergedProps);
 
       if (linearEncodings.length > 0) {
         setBars(theBars);
@@ -161,28 +177,6 @@ export default function Barcode(props) {
   );
 }
 
-Barcode.propTypes = {
-  value: PropTypes.string,
-  format: PropTypes.oneOf(Object.keys(barcodes)),
-  singleBarWidth: PropTypes.number, // width
-  maxWidth: PropTypes.number,
-  height: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
-  lineColor: PropTypes.string,
-  backgroundColor: PropTypes.string, // background
-  onError: PropTypes.func,
-};
-
-Barcode.defaultProps = {
-  value: '',
-  format: 'CODE128',
-  singleBarWidth: 2, // width
-  maxWidth: undefined,
-  height: 100,
-  lineColor: '#000000',
-  backgroundColor: '#FFFFFF',
-  onError: undefined,
-};
-
 const styles = StyleSheet.create({
   errorMessage: {
     flex: 1,

This issue body was partially generated by patch-package.

boldwade avatar Dec 12 '24 17:12 boldwade

So do I copy this in my node_modules/react-native-barcode-svg โ€” and it fixes the defaultprop error Iโ€™m getting?

I donโ€™t need to install patch-package??

If so thank you ๐Ÿ™

hussain-almuflahi avatar Mar 15 '25 01:03 hussain-almuflahi