1

I need to ask this question because tried to fix that issue couple of hours. Now I created a simple layout and this is has a sidebar. There is a navigation in this block. I'm trying to get list of coins and iterate them in to list element.

sidebar.jsx

export default {

    Sidebar() {
        return (
            <div>
                <h3>Coins</h3>
                {
                    axios({
                        method: "GET",
                        url: "./ticker.json",
                    }).then(function (resp) {

                        let list = resp.data;
                        return Object.keys(list).map((key) => {
                            console.log(list[key].name);
                            return (<li key={key}>{list[key].name}</li>)
                        });

                    }).catch(function (errors) {

                        alert("Error thrown");
                    })
                }
            </div>
        )
}

I imported that sidebar.jsx in App.js, you can see whole App.js below

import Landing from './views/landing';
import Header from './components/header';
import Sidebar from './components/sidebar';

function App() {
    return (
        <div className="container-fluid">
            {Header()}
            <div className="row">
                <div className="col-md-2">
                    {Sidebar.Sidebar()}
                </div>
                <div className="col-md-10">
                    {Landing(logo)}
                </div>
            </div>

        </div>
    );
}

export default App;

I'm getting below error. I can't understand how to fix it.

Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead.

I know axiosreturn Promise but I don't understand how to handle it. If someone could help me, that I'll much appreciated.

5
  • 2
    Please before anything else just read through reactjs.org/docs/faq-ajax.html Commented Sep 16, 2019 at 12:58
  • Why you want to call axios inside jsx doesn't make any sense Commented Sep 16, 2019 at 13:00
  • @anikislamShojib I just started to learn and develop something on react. Try to understand the life cycle. I think JSX files are separate components for react. Then I decided to use axios in JSX files. Commented Sep 16, 2019 at 13:04
  • @AlexanderStaroselsky Ooops I missed that doc. Thanks for the link. Commented Sep 16, 2019 at 13:04
  • Don't use axios inside jsx you can use componentDidMount in Cass component and useEffect in functional for Http call Commented Sep 16, 2019 at 13:06

1 Answer 1

3

Imperative code inside functional components must go inside useEffect or similar custom hooks. You could store the response in a state variable. And render only when the data get's resolved.

const App = () =>{
    const [data, setData] = useState(null)

    useEffect(() =>{
        axios.get(url)
            .then(res => setData(res.data))
    },[])


    return(
        <div>
           {
            data &&  Object.keys(data).map((key) => {
               console.log(list[key].name);
               return (<li key={key}>{list[key].name}</li>)
            })
            }
        </div>
    )
} 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I made little changes on the code but this is working for me. Also I read the doc, presented by Alexander in the question's comment block. Document also helped me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.