1

I am wanting to pass an array of string's into a react component with JSS Sitecore. At the moment, I am having to use CommonFieldTypes.SingleLineText in the sitecore component definition and splitting the string passed in on the ', ' to create an array within the actual React component.

Is there a field type where I can pass in a string array in the yml file to the component structure without the need to then turn it into an array in the component like so?

- componentName: ProgressBar
fields:
    data: ["1. test", "2. test", "3. Test"]
0

For resolving similar kind of situation I used .parse() and .map()

For Example:

import React, { Component } from "react";

export class Example1 extends Component {
    constructor(props) {
        super(props);
        this.state = {
            stringData: '["1. test", "2. test", "3. Test"]'
        };
    }

    render() {
        // Parsed valued from string
        const valuesArray = JSON.parse(this.state.stringData);

        return (
            <>
                <div>
                    <h3>Using local JSON file Array</h3>
                    <ul>
                        {valuesArray.map(item => {
                            return <li>{item}</li>;
                        })}
                    </ul>
                </div>
            </>
        );
    }
}

export default Example1;

Let me know if that helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.