How to render when the function is a character string

后端 未结 2 1680
你的背包
你的背包 2021-01-21 10:36

I\'m having trouble with React\'s Render processing. There is a function called func1 and func2, which is called when rendering (eg: return ()).

相关标签:
2条回答
  • 2021-01-21 11:06

    Firstly, React components name must begin with uppercase character, check this question for more details

    React - User-Defined JSX Components Not rendering

    Secondly instead of storing

    {funcName: "Func1"}
    

    you would store

    {funcName: Func1}
    

    and then render like

    render() {
        const Func1  = obj.funcName;
        return <Func1/>
    }
    

    or if you can't replace the string with component in your data, you would do

    render() {
        const Func1  = eval(obj.funcName);
        return <Func1/>
    }
    
    0 讨论(0)
  • 2021-01-21 11:09

    Instead of putting it as element <func2 /> call the function func2()
    But you func2 must return a valid react element.

    0 讨论(0)
提交回复
热议问题