简体   繁体   English

如何为 state 数组中的每个项目添加 React JSX?

[英]How to add React JSX for every item in state array?

I am new to React states.我是 React 状态的新手。 I have learned props and am now learning states.我已经学习了道具,现在正在学习状态。 I'm a bit confused right now.我现在有点困惑。 I am creating a web app, where you can store your code snippets in an organized place.我正在创建一个 web 应用程序,您可以在其中将代码片段存储在有条理的地方。 Without using states, I was able to create dummy data for the snippets using arrays.在不使用状态的情况下,我能够使用 arrays 为片段创建虚拟数据。 It looks like this: (pay attention to the snippets array, and then look inside runCallBack() in the JSX code to see how I am currently displaying snippets via dummy data. runCallBack() is my way of running JS code within my JSX code.它看起来像这样:(注意片段数组,然后查看 JSX 代码中的 runCallBack() 内部,看看我当前如何通过虚拟数据显示片段。runCallBack() 是我在 JSX 代码中运行 JS 代码的方式.

export class Content extends React.Component {
    render() {
        const runCallback = (cb) => {
            return cb();
        }    

        const collections = [
            {title: 'ReactJS', color: 'red'},
            {title: 'HTML', color: 'cyan'},
            {title: 'CSS', color: 'pink'}
        ]

        const snippets = [
            {title: 'Generate ReactJS component', date: new Date(), code: "<code>Code goes here</code>", language: 'javascript', collection: collections[0]},
            {title: 'CSS Grow on hover', date: new Date(), code: "<code>Other code goes here</code>", language: 'css', collection: collections[2]}
        ]


        return (
            <div className="content">
                <h1 className="content-header">Snippets</h1>
                <div className="w-layout-grid grid">
                    {
                        runCallback(function() {
                            const row = [];
                            for (var i = 0; i < snippets.length; i++) {
                                row.push(<Snippet title={snippets[i].title} code={snippets[i].code} date={snippets[i].date} language={snippets[i].language} collection={snippets[i].collection}/>) 
                            }
                            return row;
                        })
                    }
                    
                </div>
            </div>
        )
    }
}

Well, now I am trying to store snippets inside of state.好吧,现在我正在尝试将片段存储在 state 中。 When I run my code, nothing shows up on the page.当我运行我的代码时,页面上没有显示任何内容。 It makes all components disappear so obviously it isnt working.它使所有组件消失,因此显然它不起作用。 But here's the non-working code here:但这里的非工作代码:

export class Content extends React.Component {
    constructor(props) {
        super(props)
        const collections = [
            {title: 'ReactJS', color: 'red'},
            {title: 'HTML', color: 'cyan'},
            {title: 'CSS', color: 'pink'}
        ]
        this.state = {
            snippets: [
                {title: 'Generate ReactJS component', date: new Date(), code: "<code>Code goes here</code>", language: 'javascript', collection: collections[0]},
                {title: 'CSS Grow on hover', date: new Date(), code: "<code>Other code goes here</code>", language: 'css', collection: collections[2]}  
            ]
        }
    }
    render() {
        const runCallback = (cb) => {
            return cb();
        }    

        return (
            <div className="content">
                <h1 className="content-header">Snippets</h1>
                <div className="w-layout-grid grid">
                    {
                        runCallback(function() {
                            const row = []
                            const snippets = this.state.snippets
                            for (var i = 0; i < snippets.length; i++) {
                                row.push(<Snippet title={snippets[i].title} code={snippets[i].code} date={snippets[i].date} language={snippets[i].language} collection={snippets[i].collection}/>) 
                            }
                            return row;
                        })
                    }
                    
                </div>
            </div>
        )
    }
}
export default Content

Sorry if this is a stupid question , but I am just now learning states.抱歉,如果这是一个愚蠢的问题,但我现在正在学习状态。 I was able to use states in other situations successfully, but not this scenario.我能够成功地在其他情况下使用状态,但不是这种情况。 I have also heard of Redux and using that for storing state data, but I am just learning one thing at a time.我也听说过 Redux 并使用它来存储 state 数据,但我一次只学习一件事。

The problem is that you're trying to access this inside a function .问题是您试图在function中访问this In that case, this will be the function, and not the component.在这种情况下, this将是 function,而不是组件。 Thus, you won't be able to access the state .因此,您将无法访问state

Try using an arrow function instead.尝试改用箭头 function。 You can do that just by replacing runCallback(function() { with runCallback(() => {您只需将runCallback(function() {替换为runCallback(() => {

You should use the arrow inside of "runCallback".您应该使用“runCallback”内的箭头。 Regular functions override have their own root "this", while arrow function do not.常规函数覆盖有自己的根“this”,而箭头 function 没有。

Maybe checkout this article for more info - https://levelup.gitconnected.com/arrow-function-vs-regular-function-in-javascript-b6337fb87032也许查看这篇文章了解更多信息 - https://levelup.gitconnected.com/arrow-function-vs-regular-function-in-javascript-b6337fb87032

Instead of your callback function, just generate the JSX using a.map method on your state array.而不是您的回调 function,只需在 state 数组上使用 a.map 方法生成 JSX。

this.state.snippets.map((snippet) => {
   return (<JSX>...</JSX>)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM