简体   繁体   English

反应:解析错误:意外的令牌,预期的“(”

[英]React: Parsing error: Unexpected token, expected “(”

i am getting Parsing error: Unexpected token, expected "(" .我收到Parsing error: Unexpected token, expected "("

I have no idea where i'm getting this unexpected error.我不知道我在哪里得到这个意外错误。 anyways i'm probably new to reactJS.无论如何,我可能是 reactJS 的新手。 It would be great if anybody could figure out where i'm getting this unexpected error.如果有人能弄清楚我在哪里得到这个意外错误,那就太好了。 thank you so much in advance.非常感谢你。

在此处输入图像描述

./src/components/listing/Search.js: ./src/components/listing/Search.js:


function PostListPageByUser() {
    const [posts, setPost] = useState([]);
    const [userId, setUserId] = useState([]);
    let signal = axios.CancelToken.source();

    function handleChange(event) {
        setUserId(event.target.value);
    }

    function handleClick = (event) => {
        axios.get("http://localhost:8000/api/car/p_list?search=" + event, {
            cancelToken: signal.token,
        })
            .then(res => {
                const posts = res.data;
                setPost(posts);
            }).catch(err => {
            console.log(err);
        });
    }
    return (
        <React.Fragment>
        <section class="product_list section_padding">
        <div class="container">
            <div class="row">
                <div class="col-md-3">
                    <div class="product_sidebar">
                        <div class="single_sedebar">
                            <form>
                                <input type="text" name="search" onChange={handleChange} placeholder="Search keyword"/>
                                <i class="ti-search" onClick={handleClick}></i>
                            </form>
                        </div>
                    </div>
                </div>
                <div class="col-sm-9">
                    <div class="product_list">
                        <div class="row"> <br/><br/><br/>

                        {
                            posts.map((post) => {<ul key={post.id}>
                                <div class="col-lg-8 col-xl-9">

                                    <img src={post.product_image} alt="" class="img-fluid" />
                                    <h3>{post.title}</h3>

                                </div>
                            </ul>})
                        }

                        </div>
                    </div>
                </div>
            </div>
        </div>
        </section>
        </React.Fragment>
    );
}

I see 2 issues with your snippet.我发现您的代码段有 2 个问题。

Firstly, since you are using an arrow function for handleClick , you need to change it to:首先,由于您对handleClick使用箭头 function ,因此您需要将其更改为:

    const handleClick = (event) => {
        axios.get("http://localhost:8000/api/car/p_list?search=" + event, {
            cancelToken: signal.token,
        })
        .then(res => {
            const posts = res.data;
            setPost(posts);
        }).catch(err => {
        console.log(err);
    });

Secondly,第二,

{
    posts.map((post) => {
        return(
            <ul key={post.id}>
                <div class="col-lg-8 col-xl-9">
                <img src={post.product_image} alt="" class="img-fluid" />
                <h3>{post.title}</h3>
                </div>
            </ul>
        )
    })
}

As an aside, the ul tag is misused here.顺便说一句, ul标签在这里被滥用了。 You should use a div instead.您应该改用div That should not stop your code from working though but for the sake of knowledge and working in a production environment, it's important you always use the right tags.这不应该阻止你的代码工作,但为了知识和在生产环境中工作,你总是使用正确的标签很重要。 You can learn more here你可以在这里了解更多

you need to change this part你需要改变这部分

const handleClick = (event) => {
        axios.get("http://localhost:8000/api/car/p_list?search=" + event, {
            cancelToken: signal.token,
        })
            .then(res => {
                const posts = res.data;
                setPost(posts);
            }).catch(err => {
            console.log(err);
        });
    }

you cannot use the function and arrow function syntax simultaneously!您不能同时使用functionarrow function语法!

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

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